# CakePHP 3 routing prefix without subdirectory/subnamespace

**URL:** <https://discourse.cakephp.org/t/cakephp-3-routing-prefix-without-subdirectory-subnamespace/226>\
**Category:** Need Help\
**Created:** [May 2, 2016, 8:26pm UTC](https://discourse.cakephp.org/t/cakephp-3-routing-prefix-without-subdirectory-subnamespace/226 "2016-05-02T20:26:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kicaj](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kicaj/32/111_2.png) [@kicaj](https://discourse.cakephp.org/u/kicaj)\
**Post date:** [May 2, 2016, 8:26pm UTC](https://discourse.cakephp.org/t/cakephp-3-routing-prefix-without-subdirectory-subnamespace/226/1 "2016-05-02T20:26:19Z")

</div>

I looking-for solution for using CakePHP 3 routing prefix/scope for using one controller in src/Controllers for action with and without prefix.

Example:  
/admin/users/index and /users/index both works with src/Controller/UsersController

In `beforeFilter` I will be check when using prefix.

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [May 3, 2016, 2:28am UTC](https://discourse.cakephp.org/t/cakephp-3-routing-prefix-without-subdirectory-subnamespace/226/2 "2016-05-03T02:28:06Z")

</div>

Standard prefixes won’t really work for this. Instead you could use multiple routes pointing at the same controller/actions. However, unless there are additional parameters to distinguish the routes you’ll have a hard time generating `/admin` URLs.

```auto
Router::scope('/', function ($routes) {
  $routes->connect('/users/:action', ['controller' => 'Users']);
});

// You can't use 'prefix' as that means something in CakePHP
Router::scope('/admin', ['admin' => true], function ($routes) {
  $routes->connect('/users/:action', ['controller' => 'Users']);
});

```

That should work, and will let you generate reverse routes by including `admin => true` in your route arrays.

---

<div class="post-metadata">

**Author:** ![kicaj](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kicaj/32/111_2.png) [@kicaj](https://discourse.cakephp.org/u/kicaj)\
**Post date:** [May 7, 2016, 8:11pm UTC](https://discourse.cakephp.org/t/cakephp-3-routing-prefix-without-subdirectory-subnamespace/226/3 "2016-05-07T20:11:12Z")

</div>

That’s work, but when i create simple link

```
echo $this->Html->link('Test admin', [
	'action' => 'test'
]);

```

The `admin` is not included, but should be by default (when I’m on URL with admin

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [July 9, 2016, 2:26am UTC](https://discourse.cakephp.org/t/cakephp-3-routing-prefix-without-subdirectory-subnamespace/226/4 "2016-07-09T02:26:31Z")

</div>

Custom parameters don’t automatically persist, or apply to generated URLs. You’ll have to manually pass them in, or use [persistent parameters](http://book.cakephp.org/3.0/en/development/routing.html#creating-persistent-url-parameters).
