Codeigniter: send all requests that do not match the controller name to the default controller

I am working on a project with two types of URLs, one of which follows standard CI patterns

http://fancysite.com/controller/

And one more following scheme:

http://fancysite.com/category

I would like the second to call the default controller handlecategory(or something similar) with categoryas an argument.

Bonus love, if you could also tell me how to allow URLs like http://place.fancysite.com/ to call the same function, passing only placeas an argument if no category follows this URL or both placeand categoryif so.

Additional date: I already know the names of all the controllers, places, categories.

+3
source share
1 answer

To achieve this, you can use the URI routing code link -

Insert this at the end of the route file -

$route[':any'] = 'path of the default controller';

All other routes should be placed above the upper code.

Let's say the URL you encounter is

http://fancysite.com/category

So first, codeigniter will look for whether there is a controller named category. if he does not receive this, then he will check the route file to verify that there is any route indicated as follows:

$route['category/:any'] = 'actual path';

If no such route is specified, then codeigniter sends this; request the path to the default controller specified in the last line of routes.

, , .

- .

'category' URL, , URL codeigniters URI , -

$this->uri->segment(1);
+6

Source: https://habr.com/ru/post/1781617/


All Articles