# "partially" named routes

**URL:** <https://discourse.cakephp.org/t/partially-named-routes/6376>\
**Category:** Need Help\
**Created:** [July 15, 2019, 10:09pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376 "2019-07-15T22:09:58Z")\
**Posts on this page:** 14\
**Page:** 1

<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:** [July 15, 2019, 10:09pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/1 "2019-07-15T22:09:58Z")

</div>

Hii there,

I’m trying to cleanup my app a bit and I wanted to start using named routes.  
For this, I have written the current route in my `routes.php`:

```php
Router::scope('/api',['_namePrefix' => 'api:'], function($routes) {
  // V1 API
  $routes->scope('/v1', ['_namePrefix' => 'v1:'], function($routes) {
    // Projects
    $routes->scope('/projects',function($routes) {
      $routes->get('/edit-release', ['controller' => 'ProjectsRest', 'action' => 'getProjects']);
      $routes->get('/edit-release/:release', ['controller' => 'ProjectsRest', 'action' => 'getProjects'])
        ->setPass(['release']);
    ]);
  ]);
]);

```

Now I wanted to have the route in my view (for the ajax on the page) be something like this:

```auto
$this->Url->build(['_name' => 'api:v1:edit-release']);

```

However, this obviously doesn’t work since the route itself isn’t also named like this:

```auto
$routes->get('/edit-release', ['controller' => 'ProjectsRest', 'action' => 'getProjects'], 'edit-release');

```

Now before you ask: why I have the route in there twice, this is because the route builder refuses to give the appropriate route if I wanted to call it without a release (as the `:release` variable is defined in the JavaScript on the page).

So now my question is: how can I make these “partial” named routes work?  
All I really care about is the `api:v1` part.

---

<div class="post-metadata">

**Author:** ![karmicdice](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/karmicdice/32/1298_2.png) [@karmicdice](https://discourse.cakephp.org/u/karmicdice)\
**Post date:** [July 16, 2019, 7:52am UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/2 "2019-07-16T07:52:34Z")

</div>

My question is…

Why do you want to complicate the code, isn’t life complicated enough?

Should hit you hard 😃

Regarding your question, I am not sure if _ **hierarchical** _ **routes** is possible.  
But you can definately break it down into plugins and then do your thing.  
Sounds easy?

---

<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:** [July 16, 2019, 9:51am UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/3 "2019-07-16T09:51:18Z")

</div>

> [@karmicdice](#):
>
> Why do you want to complicate the code, isn’t life complicated enough?

But then what is your suggested way of doingsomething like this? 😛

---

<div class="post-metadata">

**Author:** ![karmicdice](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/karmicdice/32/1298_2.png) [@karmicdice](https://discourse.cakephp.org/u/karmicdice)\
**Post date:** [July 16, 2019, 4:14pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/4 "2019-07-16T16:14:03Z")

</div>

Haha

I would suggest breaking the app down into plugins. As opposed to Zend Framework. CakePHP has a Hierarchical system but the workaround is via plugins…  
Just enable bootstrap and routes in your plugin and it will be able to accomplish the nested routes!

---

<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:** [July 16, 2019, 5:22pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/5 "2019-07-16T17:22:23Z")

</div>

Well, nesting the routes works perfectly fine, it’s just that naming like this doesn’t work like I hoped.

---

<div class="post-metadata">

**Author:** ![karmicdice](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/karmicdice/32/1298_2.png) [@karmicdice](https://discourse.cakephp.org/u/karmicdice)\
**Post date:** [July 16, 2019, 5:42pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/6 "2019-07-16T17:42:38Z")

</div>

Also reverse calling wont work either.

Just to accomplish that, I would suggest breaking it down into plugins… that is… if your client has enough budget and you, enough time.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [July 16, 2019, 6:11pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/7 "2019-07-16T18:11:56Z")

</div>

you can create one route with pattern that accepts empty string

```auto
$routes->get('/edit-release/:release', ['controller' => 'ProjectsRest', 'action' => 'getProjects'], 'edit-release')
                ->setPass(['release'])
                ->setPatterns(['release' => '[a-z]{0,3}']);

```

gotcha is you need to suply `release` param even when its empty

```auto
<?= $this->Html->link('test1', ['_name' => 'api:v1:edit-release', 'release' => '']) ?>
<?= $this->Html->link('test2', ['_name' => 'api:v1:edit-release', 'release' => 'a']) ?>

```

---

<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:** [July 16, 2019, 8:06pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/8 "2019-07-16T20:06:01Z")

</div>

> [@karmicdice](#):
>
> if your client has enough budget and you, enough time.

Well… considering it’s my own website, I think I have plenty of budget for it XD

> [@Graziel](#):
>
> gotcha is you need to suply `release` param even when its empty

Hmm… I was afraid I had to do this yes… I’ll open an issue on the CakePHP repo to see if maybe they could add something that prevents these cases (because I doubt that I’m the only one that ever has ran into this).  
Thanks anyways, I’ll have a closer look into it.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [July 17, 2019, 7:17am UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/9 "2019-07-17T07:17:36Z")

</div>

if you dont need any pattern checking you might also use greedy one

```auto
$routes->get('/edit-release/*', ['controller' => 'ProjectsRest', 'action' => 'getProjects'], 'edit-release');

```

```auto
<?= $this->Html->link('test1', ['_name' => 'api:v1:edit-release']) ?>
<?= $this->Html->link('test2', ['_name' => 'api:v1:edit-release', 'aa']) ?>

```

and in controller

```auto
$this->request->getParam('pass.0');

```

---

<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:** [July 17, 2019, 2:53pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/10 "2019-07-17T14:53:04Z")

</div>

my function looks like this:

```php
public function editRelease($release = null) {
}

```

But I suppose that with the greedy route, this won’t work?

little sidenote: the action I want to use is actually called editRelease (I just spotted this issue in my code when testing some stuff lol)

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [July 17, 2019, 3:54pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/11 "2019-07-17T15:54:39Z")

</div>

i think it should work IIRC you just lose ability to use `->setPattern` this could be annoying if you tried to pass 2 params in one url segment ie `/:id-:slug` but should still work if you give multiple arguments

```auto
public function editRelease($arg1 = null, $arg2 = null) {
}

```

[https://book.cakephp.org/3.0/en/development/routing.html#routes-configuration](https://book.cakephp.org/3.0/en/development/routing.html#routes-configuration)

> The trailing `/*` tells the router to pass any additional segments as method arguments. For example, `/users/view/123` would map to `UsersController->view(123)`

---

<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:** [July 17, 2019, 4:22pm UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/12 "2019-07-17T16:22:35Z")

</div>

Hmm… I see.  
Well, considering the app requires specific params at specific places anyways that isn’t too much of an issue.  
I’ll have a go at it when I’m home again 🙂

---

<div class="post-metadata">

**Author:** ![karmicdice](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/karmicdice/32/1298_2.png) [@karmicdice](https://discourse.cakephp.org/u/karmicdice)\
**Post date:** [July 19, 2019, 5:29am UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/13 "2019-07-19T05:29:53Z")

</div>

Curious though… What are you making?

---

<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:** [July 19, 2019, 7:37am UTC](https://discourse.cakephp.org/t/partially-named-routes/6376/14 "2019-07-19T07:37:56Z")

</div>

trying to clean-up some stuff (mainly my API) 🙂

> <https://gitlab.com/FinlayDaG33k/Website/blob/master/www/config/routes.php#L120>
