URLs are routed in the following order:
- Explicit routes in
$route (routes.php) are checked in order. - The implicit route
[folder/]controller/methodname/args... used as a backup attempt.
If you have a small number of known explicit routes, you can simply add them to $route :
$route['(my-slug|my-other-slug|my-third-slug)'] = 'pages/slug_on_the_fly/$1'
(The route keys are actually parsed because regular expressions with :any and :num overwritten with .+ And [0-9]+ .)
If you have a large number of such routes (maybe this is not a good idea, BTW!), You can simply add a wildcard at the end of $route :
$route['([^/]+)/?'] = 'pages/slug_on_the_fly/$1'
A regular expression means "any URL that does not have slashes (except maybe the last one)". You can clarify this to describe your slug format if you have other restrictions. (The good one is [a-z0-9-]+ .) If your controller detects a bullet in db, you're done. If it is not, it should serve 404.
However, you refuse the possibility of any implicit routing, since Codeigniter does not provide any way for the controller to βrefuseβ the route back to the router. For example, if you have a controller named 'foo' and you want a URL such as /foo to go to Foo::index() , you must add an explicit route for this case, because it will be caught by this route and sent to Pages::slug_on_the_fly('foo') instead. In general, you should not have bullets, which are also controller class names! That's why you should have very few such URLs, if you don't have any!
If you have a large number of these explicit routes, and you do not want to observe these implicit routing restrictions, you can try adding them dynamically to $route :
- Create a
routes_extra.php file that routes.php includes. Write new routes for it as part of saving the page or when creating / deploying it. - Subclass
Router.php and add a new routing layer. - Add a
pre_system hook that adds routes.
I am sure there are other ways.
source share