# How to add a base path to all routes?

**URL:** <https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125>\
**Category:** Components & MVC\
**Tags:** laminas-mvc, laminas-router\
**Created:** [February 23, 2021, 7:13pm UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125 "2021-02-23T19:13:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![renanliberato](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/renanliberato/32/904_2.png) [@renanliberato](https://discourse.laminas.dev/u/renanliberato)\
**Post date:** [February 23, 2021, 7:13pm UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/1 "2021-02-23T19:13:32Z")

</div>

Hello!  
I’m deploying a laminas app in a subfolder under my domain ([https://renanliberato.com.br/myapp](https://renanliberato.com.br/myapp)), and I don’t know exactly how to deal with this “/myapp” prefix on my app.  
Currently, I’m adding this base path into all my routes configurations, like this:

```auto
return [
    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route' => '/myapp/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action' => 'index',
                    ],
                ],
            ],
        ],
        ...
    ],
    ...
];

```

However, it seems wrong to add this information directly in the routes. I’d expect to have a way to setup it in local.php or global.php, so that when I’d want to deploy it on a subdomain or a separate domain, I’d not need to rewrite all the routes.

For view paths (for static assets), I could find how to do it using ‘view\_manager’ \> ‘base\_path’ in the config and ‘$this-\>basePath()’ in the templates, but nothing for routes.

Do you guys know If it is possible via laminas config, or via .htaccess, or if I should not care at all about it (hehe)?

---

<div class="post-metadata">

**Author:** ![williamgall](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@williamgall](https://discourse.laminas.dev/u/williamgall)\
**Post date:** [February 23, 2021, 10:30pm UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/2 "2021-02-23T22:30:42Z")

</div>

you could always create a constant and then use that and if you decided later to get rid of the baseroute you could just define it as an empty string

define(“BASEPATH”,"/myapp"); // in public/index.php

then in module.config.php  
‘route’ =\> BASEPATH.’/Module/’,

then if you later get rid of basepatch just define it as “”

Hope that helps

---

<div class="post-metadata">

**Author:** ![renanliberato](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/renanliberato/32/904_2.png) [@renanliberato](https://discourse.laminas.dev/u/renanliberato)\
**Post date:** [February 24, 2021, 3:16am UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/3 "2021-02-24T03:16:44Z")

</div>

Oh, that’s right, it solves my problem!

I think something integrated into the framework would help when using (external) Laminas modules that also define routes, but it will for sure be enough for me.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [February 24, 2021, 7:07am UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/4 "2021-02-24T07:07:05Z")

</div>

> [@renanliberato](#):
>
> …or if I should not care at all about it (hehe)?

Everything is already there. Please check the comment in the `.htaccess` file of the skeleton application:

> <https://github.com/laminas/laminas-mvc-skeleton/blob/12ff936b69caf63797b9c06a9f3a5d95482be913/public/.htaccess#L10-L15>

> [@renanliberato](#):
>
> I think something integrated into the framework would help when using (external) Laminas modules that also define routes…

Please remember, because someone writes something here, it is not the official or only solution. 😉

Using global constants was the solution in version 1 of the Zend Framework but now it is no longer recommended.

---

<div class="post-metadata">

**Author:** ![williamgall](https://avatars.discourse-cdn.com/v4/letter/w/b77776/32.png) [@williamgall](https://discourse.laminas.dev/u/williamgall)\
**Post date:** [February 24, 2021, 11:59am UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/5 "2021-02-24T11:59:51Z")

</div>

never said mine was the official solution. It was just a solution. Thanks for pointing this out.

---

<div class="post-metadata">

**Author:** ![renanliberato](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/renanliberato/32/904_2.png) [@renanliberato](https://discourse.laminas.dev/u/renanliberato)\
**Post date:** [February 24, 2021, 12:23pm UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/6 "2021-02-24T12:23:46Z")

</div>

> Everything is already there. Please check the comment in the `.htaccess` file of the skeleton application:

Yeah, I noticed that it helps in the index.php script resolution. However, I still need to include the subdirectory in my routes.

> Please remember, because someone writes something here, it is not the official or only solution. 😉

Sure. I should not have marked it as a solution on first hand. Not because @williamgall specifically posted it, but because it was the first and only one in a short timespan.

> Using global constants was the solution in version 1 of the Zend Framework but now it is no longer recommended.

I’m not sure if another solution will fit what I want (or if I should expect it from the framework, anyway)… The simplest case that I’m referring too is:

My app is hosted on a “myapp” subdirectory. So, I’d like to, when declaring a route ‘/about’, the user would be able to access it by going to “[https://domain.com/myapp/about](https://domain.com/myapp/about)”. However, it does not seem to work… I must implement my route like ‘/myapp/about’ otherwise I get a 404 error.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [February 24, 2021, 1:08pm UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/7 "2021-02-24T13:08:30Z")

</div>

> [@renanliberato](#):
>
> So, I’d like to, when declaring a route ‘/about’, the user would be able to access it by going to “[https://domain.com/myapp/about”](https://domain.com/myapp/about%E2%80%9D).

The base path and base URL is detect by `Laminas\Http\PhpEnvironment\Request` and they are used in router to create the correct URLs.

For your application it is `/myapp`.

Try the following in the layout script:

```php
<?= $this->basePath() ?>
<?= $this->url('home') ?>

```

And in the output `/myapp` should be included.

---

<div class="post-metadata">

**Author:** ![renanliberato](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/renanliberato/32/904_2.png) [@renanliberato](https://discourse.laminas.dev/u/renanliberato)\
**Post date:** [February 24, 2021, 2:45pm UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/8 "2021-02-24T14:45:29Z")

</div>

Oh, so I may be missing something.

I’m gonna check my subfolder htaccess that redirects the requests to public folder.

Currently, my Request basePath is empty.

---

<div class="post-metadata">

**Author:** ![renanliberato](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/renanliberato/32/904_2.png) [@renanliberato](https://discourse.laminas.dev/u/renanliberato)\
**Post date:** [February 25, 2021, 1:31am UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/9 "2021-02-25T01:31:51Z")

</div>

Hey, I could not find exactly what is making my basePath be empty (probably some deploy specifcness), so I extended the RequestFactory and set both basePath and baseUrl manually (via a local.php config, so at least not hard coded).

That’s enough for now, as both routes and assets resolutions are working.

I’ll try to understand better why the basePath resolution is resolving to empty later on.

Thanks for the kind help, you all.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [February 25, 2021, 7:40am UTC](https://discourse.laminas.dev/t/how-to-add-a-base-path-to-all-routes/2125/10 "2021-02-25T07:40:07Z")

</div>

> [@renanliberato](#):
>
> I’ll try to understand better why the basePath resolution is resolving to empty later on.

Try to debug the methods `Laminas\Http\PhpEnvironment\Request::detectBasePath()` and `Laminas\Http\PhpEnvironment\Request::detectBaseUrl()` and you will find the answer.
