# Dynamic routes?

**URL:** <https://discourse.cakephp.org/t/dynamic-routes/5740>\
**Category:** Need Help\
**Created:** [March 6, 2019, 2:16pm UTC](https://discourse.cakephp.org/t/dynamic-routes/5740 "2019-03-06T14:16:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [March 6, 2019, 2:16pm UTC](https://discourse.cakephp.org/t/dynamic-routes/5740/1 "2019-03-06T14:16:12Z")

</div>

cakephp 3.x

Is there a way to create dynamic routes per article ?  
(like in some well known CMS)

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [March 7, 2019, 6:55am UTC](https://discourse.cakephp.org/t/dynamic-routes/5740/2 "2019-03-07T06:55:19Z")

</div>

This is what I use for it:

```auto
$routes->connect('/blog/*',['plugin'=>'Kikioboeru/Blog','controller' => 'Blog', 'action' => 'view']);

```

context:

```auto
Router::scope('/', function (RouteBuilder $routes) {
  $routes->connect('/blog/*',['plugin'=>'Kikioboeru/Blog','controller' => 'Blog', 'action' => 'view']);
});

```

It routes `/blog/some-blog-post` to the `Blog` controller in `Kikioboeru/Blog` plugin with the `view` action.

Refer to the documentation for more info:  
[https://book.cakephp.org/3.0/en/development/routing.html](https://book.cakephp.org/3.0/en/development/routing.html)

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [March 7, 2019, 9:30am UTC](https://discourse.cakephp.org/t/dynamic-routes/5740/3 "2019-03-07T09:30:42Z")

</div>

Thank you for answering.

I should have been more precise. Sorry.

I’ve been using cakephp since 1.2 so I know how to use the router in the routing section.

What I am looking for is a way to write routes in a controller method.  
So that [www.domain.com/articles/id](http://www.domain.com/articles/id) reads [www.domain.com/what\_I\_want](http://www.domain.com/what_I_want) in the address bar.

---

<div class="post-metadata">

**Author:** ![liqueurdetoile](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/liqueurdetoile/32/1111_2.png) [@liqueurdetoile](https://discourse.cakephp.org/u/liqueurdetoile)\
**Post date:** [March 8, 2019, 11:26am UTC](https://discourse.cakephp.org/t/dynamic-routes/5740/5 "2019-03-08T11:26:01Z")

</div>

Basically, CakePHP needs to know which controller, action and param to use to process request and send a response. It is the router job.

There is many ways to achieve permalinks all you have to do is :

- Keep a map between slugs and records in database
- Set up a scheme to disambiguate permalinks calls from others requests
- Use a custom route to get slug param because `id` param must be integer or UUID.

If the controller is always implicit (`ArticlesController` in your example) and the action is also always implicit (`view` in your example), you can simply set a custom route like this:

```
$routes
  ->connect('/:slug', ['controller' => 'Articles', 'action' => 'view'])
  ->setPass(['slug']);

```

The `slug` param will be available in the view actions as parameter (like `id`). Then, you only need to fetch the article that matches the slug and pass it to the view.

Of course, it means that it may conflics with others controllers default actions (`/users` will look for a slug called users and not the `index` action of the `users` controller as it should do if using `DashedRoute::class` as fallback).

To avoid this, the the [cookbook example](https://book.cakephp.org/3.0/en/development/routing.html#configuring-route-options) keeps the `articles` controller in route :

```
$routes
   ->connect('/articles/:slug', ['controller' => 'Articles', 'action' => 'view'])
   ->setPass(['slug']);

```

It will give you better control on other application calls though allowing you to use SEO friendly urls. You can also use subdomains or routing prefix to serve requests to other controllers and keep root level only for articles.

The used scheme is up to you given your app architecture.

This is the “pure” CakePHP response. The famous “CMS”, if I guess well, is relying on `mod_rewrite` to pass slug as query param to process request.

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [March 11, 2019, 5:14pm UTC](https://discourse.cakephp.org/t/dynamic-routes/5740/6 "2019-03-11T17:14:46Z")

</div>

Thank you for your answer.  
Question : if I pass a slug as parameter like in :  
[www.domain.com/articles/slug\_string](http://www.domain.com/articles/slug_string)  
what difference does it make compared to passing the id ?  
I guess it should be slower as ids are primary indexes in the database.  
But does it make a real difference ?

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [March 11, 2019, 6:46pm UTC](https://discourse.cakephp.org/t/dynamic-routes/5740/7 "2019-03-11T18:46:51Z")

</div>

Except the database query maybe it doesn’t do anything different for the route itself.  
`:id` just tells Cake: `pass as variable 'id'`.  
You can then (for example) limit it by saying (or rather, code it in) to the app: `only accept integers` or `only accept [a-zA-Z]`.

So yea, except for the database it shouldn’t do anything in terms of performance
