Welcome Guest, Not a member yet? Register   Sign In
Help: Want custom URI controller handling
#1

[eluser]Unknown[/eluser]
I am new to CI and have some simple questions....

I have read that URL's such as http://localhost/blog/1/2/3 etc get sent automatically to a blog controller (blog.php) with parameters 1/2/3.

I don't want this feature... I simply want every request to go to a "default" controller with the parameters, how can I achieve this?

Now, given the above, I want to forward from this "default" controller to other controllers based on other logic. How can I forward from the "default" controller to other controllers?

thanks,
John.
#2

[eluser]KPowell[/eluser]
I'm not sure if this is what you're looking for but I think that what you want is something that is very much part of the framework already.

Instead of a default controller that calls other controllers though, you would use routes as a switch for the controllers. Then from there, as long as you have an index function in the controller, you can do whatever you want within that controller using the uri class.

So, if you wanted all pages that started like "http://mysite.com/blog" to go to the blog controller and everything that starts "http://mysite.com/comments" to go to the comments controller, in your config/routes.php file you would do the following:

Code:
$route['blog/:any'] = "blog/index";
$route['comments/:any'] = "comments/index";

I've used a similar structure before, but find that mapping directly to methods (which would be whatever comes right after your controller name, ie. http://mysite.com/blog/posts). But both work well as long as you can work with the uri stuff.
#3

[eluser]Unknown[/eluser]
No I don't want to use routes in this way. I want every request to go to a front controller and the applicaion logic will process the request and/or forward to other controllers.

Using your idea, can I do this:

$route[':any'] = "controller";

And this will get processed by controller.php in the system folder?

How do I forward to other controllers?

thanks,
John.
#4

[eluser]xwero[/eluser]
The reason for this behavior is because CI doesn't keep a register of all the methods in the controllers.
Code:
$route['([a-z\-]+)/(\d+)(.*)'] = '$1/index/$2$3';
What this route does is assume all the first segments of the url are controllers, which isn't always the case. The second thing it looks for is if the second segment is a number, if it's a controller method it is a word. Then it looks if there are more segments.

I think this is a better solution than the rewrite of the router functionality you want to do.




Theme © iAndrew 2016 - Forum software by © MyBB