• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Login and Route nightmare!

[eluser]Sven Delle[/eluser]
OK, so I decided to spent the rest of the day explaining my situation, and in the process (of actually going through the process step by step) hopefully shed some light on this for myself (as I seem to waste an enormous amount of time trying to 'guess' what's wrong).

SETUP: a simple (portfolio - if you like) site for:
Quote:1. Basic pages (contact, profile etc.)
2. Cases (text, images, video - what have you)
3. Admin area (for controlling content)
4. Login area (if site is [protected] for anyone else than the client - this is a flag in a custom site_config.php file in the application/config folder.

Quote:CI 2.1.0
HMVC 5.4

Folder structure:

I have a Site Controller, a Login Controller and an Admin Controller.

The Site Controller extends a Public_Controller (which extends MY_Controller - which does absolutely nothing) which check to see if site is [protected] for anyone else than the client.

If it is, it redirects to ... well, I'll get back to that!

First thing I wanted was to get rid of the site/index prefix for all pages in an attempt to make it more SEO friendly.

So here's what I did:
I removed all links in the website that had 'site/index' in it, and added a route to the routes.php file in the application/config folder like this:

$route['(:any)'] = 'site/index/$1';

So now I could point my browser at
instead of
, and have the portfolio page displayed. Super, well done!

NOTE: Behind the scenes the Site Controller would take the parameter - 'portfolio' in this case - and use it for a database lookup to get that specific page content.

But! Now accessing the admin area like:
didn't (ofcourse) work anymore.

In my Admin_Controller I redirect to admin/admin_login' if the user is not logged in. I use an Admin_Controller to protect all Controllers in the admin area. But because of that I end up in a neverending loop.

To fix that problem I added another route to my routes.php file.

$route['admin'] = 'admin/$1';

And after much fiddling around, I ended up having to add this as well:
$route['admin/(:any)'] = 'admin/$1';
<b>And this is where the whole routing thing occured to me as being somewhat of a challenge.</b>

My setup is based on an Admin Controller which handles everything Admin, with its own admin_login procedure (Admin_login Controller), and a separate Login Controller for handling when the site is protected to other people than clients during development. This gives me the option of giving a client a password to acces the website. This is all handled by the Site Controller which extends the Public_Controller, which sends the user to a login page if:

1. Site is protected
2. User is not logged in (this will obviously be redundant in the case the site is not protected).

But as with routes I can't get to the 'login' controller unless I add it to the routes.php file.

Therefore I did, so now I have these routes:
$route['admin'] = 'admin/$1';
$route['admin/(:any)'] = 'admin/$1';

$route['login'] = 'login';

$route['(:any)'] = 'site/index/$1';

But now - no matter what I try - I can't get to the site from the Login Controller:

Doing this after validating the user (and setting $this->session->set_userdata($data) accordingly):




keep sending me back to the login page. I simply cannot see why this happens. I may either be over tired or simply plain stupid.

Additional info:
Quote:The Site Controller takes into account if no parameter is passed to it - then it gets a default page from the database.

The validated user data gets set properly (believe me). The obvious question would be of that concern - from the behavior.

I'm simply avoiding making this post longer than it already is by flooding it with complete code.

But no matter what: routes seem pretty challenging if you want to use this one to make it 'clean':
$route['(:any)'] = 'controller';

You'll have to specify ALL of your controllers afterwards.

<b>Can anyone see where I obviously don't get it?</b>

Sorry for the fairly long post.

You can use a negative rule and declare all of the controllers in it that you don't want affected, and if it isn't one of them to process your (:any) rule. Then you don't need to create additional rules for your other controllers that you don't want to have special routes.

Basically something like:
$route['((?!admin|login).*)'] = 'site/index/$1';

If the first segment is "admin" or "login", it won't match the rule so it will process as a "normal" CI request.

[eluser]Sven Delle[/eluser]
Now that made a hell of a difference!

Thanks a bunch!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.