How to add a base path to all routes?

Hello!
I’m deploying a laminas app in a subfolder under my domain (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:

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)?

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

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!

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

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

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

1 Like

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

1 Like

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. :wink:

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”. However, it does not seem to work… I must implement my route like ‘/myapp/about’ otherwise I get a 404 error.

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:

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

And in the output /myapp should be included.

1 Like

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.

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.

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

1 Like