Reputation: 5883
I'm working on a project involving two kinds of URLs, one following the standard CI pattern
http://fancysite.com/controller/
And another presenting the following scheme:
http://fancysite.com/category
I would like the second to call the default controller's handlecategory
(or something like that) function with category
as an argument.
Bonus love if you could also tell me how to let URLs like http://place.fancysite.com/ call the same function passing just place
as an argument if no category follows that URL, or both place
and category
if it does.
Additional datum: I already know the names of all controllers, places, categories.
Upvotes: 3
Views: 1982
Reputation: 5405
You can use the codeigniter's URI routing for achieving this -
Insert this at the end of your route file -
$route[':any'] = 'path of the default controller';
All the other routes should be placed above the upper code.
Lets say the URL codeigniter encountered is -
So firstly codeigniter will search if there is a controller named category. if it does not get that then it will check the route file to check that is there any route specified like this -
$route['category/:any'] = 'actual path';
If there is no such route specified in that case codeigniter will pass this request to path of the default controller you have mentioned in your last line of routes.
Even if the link contains sub-domain it doesn't matter the request will go to same default controller.
Now you can insert logic for sub-domain handling or anything else in the default controller.
You can even obtain the parameter 'category' in our url example taken which is in the URL using codeigniters URI helper class as below -
$this->uri->segment(1);
Upvotes: 6