2

Currently in my CI project I have a single controller that handles all things account. Such-as register, login, activation, etc.

My routes work as such...

domain.com/account/login/ or domain.com/account/register/

How can I remove account from the route while also being about to remove the controller from other pages.

I basically want the controller to always be removed. One of my reasons for this is SEO, search engine rank the importunateness of a page based on how deep it is in a website.

The only way I have seem to achieve this is to do some thing like route['activate'] = 'account/activate'; for every single page, which would be a huge hassle.

1
  • do you foresee any methods having identical method names? Commented Apr 13, 2011 at 1:42

5 Answers 5

4
$route['^(?!other|controllers).*'] = “account/$0″;
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me. Because of how URL's are being rewritten on my install, I needed to use $route['^(?!/other|/controllers).*'] = "controller/$0";
2

Try this :

$route['(:any)'] = "account/$1";

3 Comments

The problem with this is, that I have more than one controller.
I answered this since you mentioned "I have a single controller that handles all things account".. if you have different controllers you have to explicitly set routes for each controllers.
Ah I see sorry, but I also later mentioned that I would like to do it to all my controllers.
1

The answer to your question is that you DO have to explicitly set the routes.

How is it going to know which controller a given function is in????

You have to tell it.

2 Comments

Thats what I was thinking, but I was hoping for some magic :)
if you want magic, use rails, codeigniter expects YOU to be the magician.
1

use mod_rewrite (if the controller is always the same name)

Comments

1

Ok, I can think of one way to do this, but it is probably gonna be more of a pain than just writing out routes for each function.

You need to extend the Router.php with application/core/MY_Router.php and overide the _validate_request() method. Which basically decides if this this is a valid route or not.

it does a check to see if the controller class exists then fails if it doesn't exist. You need to replace this with some code which assumes no controller segment, then scans thru each of your controllers and checks if it contains the method called (it will be segment 1, since theres no controller).

Now the tricky part, at this point in the CI lifecycle your controller obviously isnt loaded, so you cant examine it using method_exists() yet.

You need to load your controllers one at a time, and then for each one run

method_exists($loaded_class, $method_name)

and if its true, then set then go ahead and call:

$this->set_class('the_name_of_the_scanned_class_which_had_the_method');

Then CI can keep going on as normal and it will load your methods without the user ever know what controller it loaded from.

.. probably not worth the hassle imho. A much easier solution would be to just have one controller and one route to that controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.