Francisco Manrique
Francisco Manrique

Reputation: 1

Codeigniter URL route using 404_override

I have a very big application (many controllers and modules), within the application there are articles (posts), and I want to put the URL like: example.com/[category_name]/[article_name] ie: example.com/gadgets/top-ten-coolest-gadgets-of-2018

I have a controller called Content so the URL was set up like this: example.com/content/[category_name]/[article_name]

I want to remove "/content/" from the URL, so, first I tried to add the following in routes.php:

$ route ['(:any)/(:any)'] = '/content/categoryArticles/$1/$2';

But the rest of the controllers stop working, so I had to add all them in routes.php (and there are many).

I want to try to use 404_override as a default route when the URL does not find the controller and redirect all to /content/categoryArticles ie, do this:

route ['404_override'] = '/content/categoryArticles/'

and in the categoryArticles function, explode the URL by segments and determine the parameters.

This is a good practice?

Upvotes: 0

Views: 1136

Answers (1)

ivanavitdev
ivanavitdev

Reputation: 2918

Updated Answer:

If you use (:any) as your first parameter then all controller/folder will be override by routes. however you can use an alias first then the following parameters so that it wont affect other controllers.

$routes['post/(:any)/(:any)'] = 'content/categoryArticles/$1/$2';

Unless you have a constant list of category then you can initialize those categories then add it to routes

$categories = array('gadgets', 'review', 'news', 'sports', 'business');

foreach ($categories as $category) {
    $route["{$category}/(:any)"] = "content/categoryArticles/{$category}/$1";
}

Upvotes: 0

Related Questions