# Add prefix to all routes

**URL:** <https://discourse.slimframework.com/t/add-prefix-to-all-routes/515>\
**Category:** Questions\
**Created:** [July 17, 2016, 6:35am UTC](https://discourse.slimframework.com/t/add-prefix-to-all-routes/515 "2016-07-17T06:35:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudi\_jw](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@dudi\_jw](https://discourse.slimframework.com/u/dudi_jw)\
**Post date:** [July 17, 2016, 6:35am UTC](https://discourse.slimframework.com/t/add-prefix-to-all-routes/515/1 "2016-07-17T06:35:09Z")

</div>

hi all  
i wanted to ask if there is a way to add prefix to all the routes.  
for example i have to routes:  
$app-\>get(’/users/{id}’,‘UserController:getUser’);  
$app-\>get(’/users’,‘UserController:getUsers’);

so i want the route will actualy be:  
/api/users/11  
/api/users

is there a way to achieve this without adding ‘/api/’ to all the routes?  
thanks  
dudi

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [July 17, 2016, 7:33pm UTC](https://discourse.slimframework.com/t/add-prefix-to-all-routes/515/2 "2016-07-17T19:33:34Z")

</div>

I would suggest that you define a variable $prefix which you conditionally set to either “/api” or to an empty string. That way you can do the following, depending on your exact needs:

```
$app->get("$prefix/users/{id}",'UserController:getUser');

```

Seems like an easy solution. You can then add a switch to your settings.php to configure the contents of $prefix at the beginning of your routes.php.

---

<div class="post-metadata">

**Author:** ![iansltx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/iansltx/32/153_2.png) [@iansltx](https://discourse.slimframework.com/u/iansltx)\
**Post date:** [July 17, 2016, 8:50pm UTC](https://discourse.slimframework.com/t/add-prefix-to-all-routes/515/3 "2016-07-17T20:50:00Z")

</div>

You can actually use $app-\>group($path, $callable) to achieve this. Inside the callable, you can treat $this like $app outside the callable. So you could revise your example to:

```
$app->group('/users', function() {
    $this->get('/{id}', 'UserController:getUser');
    $this->get('', 'UserController:getUsers');
});
```

---

<div class="post-metadata">

**Author:** ![geggleto](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/geggleto/32/11_2.png) [@geggleto](https://discourse.slimframework.com/u/geggleto)\
**Post date:** [July 18, 2016, 1:04pm UTC](https://discourse.slimframework.com/t/add-prefix-to-all-routes/515/4 "2016-07-18T13:04:52Z")

</div>

Yep.

```php
$app->group('/api', function () {
    $this->get('/users/{id}','UserController:getUser');
    $this->get('/users','UserController:getUsers');
});

```

The code above will result in `/api/user/{id}` and `/api/users`
