0

I am trying out CodeIgniter 2.1.4. I already have a controller for showing static pages which I built using the tutorial in CodeIgnitor documentation. I later set-up my routes like this:

// <http://localhost/> refers to <http://localhost/pages/view/>
   $route['default_controller'] = "pages/view";
// <http://localhost/somepage/> refers to <http://localhost/pages/view/somepage/>
   $route['(:any)'] = "pages/view/$1";
// .htaccess is already setup to rewrite the url without index.php

Now, I don't have much experience with PHP, and the concepts of URL Rewriting and MVC Architecture are fairly new to me.

Let's say there's are pages called •Home, •About, •Admin and •Contact.

For the pages •Home, •About and •Contact, the Pages controller works right as it should.

But for •Admin page, I want to have a separate controller which determines whether the user has logged in or not, and whether he has admin rights etc. And if he hasn't logged in yet, I should load the Login view instead of the Admin view.

The Pages controller has a fairly simple logic. It checks whether the string in argument, appended with .php and prepended with the views directory, exists as a file or not. If it doesn't show_404(), if it does, load view header-template, then load the page, then load view footer-templat. I'm pretty sure most of you who have worked with CodeIgniter must've seen a similar logic for static pages.

I could do redirect('login') inside my Admin view, but that doesn't seem to work. If I create a separate controller for Admin, how would I access it, while according to routes, every URL gets directed to pages/view controller (line#4 in the above code).

As I've already said, I'm fairly new to this. It might be some retarded mistake that I'm making. Or my whole MVC structure might be inappropriately built. How do I get past this and start worrying about the authentication stuff? Can anyone advise?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zia Ur Rehman
  • 1,141
  • 2
  • 11
  • 20

1 Answers1

2
$route['default_controller'] = "pages/view";
$route['admin/(:any)'] = "admin/$1"; //(admin controller) with "any" method 
$route['(:any)'] = "pages/view/$1";

localhost/poject/admin/edit as example

The problem you are experiencing is simple, you overwrite all controllers by that (:any) it isn't wrong but you need to manualy assign each controller that you want route as normal controller as I posted above.

please note that routes are order dependant and if one (first) is used second one is ignored. "Routes will run in the order they are defined. Higher routes will always take precedence over lower ones."

For authentification please see this post.

I would rather use _remap() and extend CI_Controller to take over my routes instead of having this route $route['(:any)'] = "pages/view/$1"; in routes.php.

remapping

Community
  • 1
  • 1
Kyslik
  • 8,217
  • 5
  • 54
  • 87
  • I was about to answer my own question. I figured the best way would be what you mentioned. I now have a `$route` assignment for every page ;). If it's *about*, *contact* or *home*, the route is `pages/view/$1`, but if it's *admin*, the route is `admin/index/$1`. But it's probably not the most efficient way. I'll work some different logics following the *first-route-first* and *`_remap()`* strategies that you mentioned. :) – Zia Ur Rehman Sep 19 '13 at 17:06