How to set a dynamic route for using slug in CodeIgniter?

Say I have a controller named

pages

and there is a method

slug_on_the_fly

public function slug_on_the_fly($slug)

What does my route look like for this?

eg. for a blog controller it would be easy:

 $route['blog/(:any)'] = 'pages/slug_on_the_fly/$1'; 

and then http://localhost/blog/name-of-the-article works fine

However, if I want to do this, as without a blog , for example,

http://localhost/name-of-the-article or http://localhost/another-article-blablabla

How to do this and not break other routes, for example. $route['friends'] = 'users'; or $route['about-us'] = 'pages/about_us'; ?

Because if I do this: $route['(:any)'] = 'pages/slug_on_the_fly/$1';

Perhaps this will ruin the rest or?

+4
source share
5 answers

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.

+6
source

You can use database driven routes.

Add the blog_slugs table to your MySQL database:

 CREATE TABLE IF NOT EXISTS `blog_slugs` ( `id` bigint(20) NOT NULL auto_increment, `slug` varchar(192) collate utf8_unicode_ci NOT NULL PRIMARY KEY (`id`), KEY `slug` (`slug`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1; 

Replace the code in application / config / routes.php as follows:

 $route[ 'default_controller' ] = 'main'; $route[ '404_override' ] = 'error404'; require_once( BASEPATH .'database/DB'. EXT ); $db =& DB(); $query = $db->get( 'blog_slugs' ); $result = $query->result(); foreach( $result as $row ) { $route[ $row->slug ] = 'pages/slug_on_the_fly/$1; } 

All you have to do is create a post when you create a blog post and everything will be ready:

 INSERT INTO `blog_slugs` (`slug`) VALUES ('name-of-the-article'); 
+4
source

Use 404 to override the reserved controller / method route . If a valid controller / route does not exist, this method will be called. Works great as a trick.

+3
source

Suppose you have 3 controllers besides the page controller, controller 1, controller 2, and controller3, then

 $route['^(?!controller1|controller2|controller3).*'] = 'pages/slug_on_the_fly/$1';; 
+1
source

Perhaps this will help you.

 $route['controllerName/([^/]+)/([^/]+)'] = "index/author/$1/$2"; 
0
source

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


All Articles