router
router copied to clipboard
Support base prefix for url generator
I want to write an application that will live under some path, e.g. /blog
So I have two ways to write my urls:
- Each route should contain
/blog
- Add a prefix to url generator that will automatically add the prefix to all further urls
If I choose the first option that I need to rewrite all my urls when I want to move from /blog
to /
.
There are more problems than I described above: proxying, making extensions and etc.
Here is a workaround now. Place that code into config/bootstrap.php
.
<?php
declare(strict_types=1);
return [
function (\Psr\Container\ContainerInterface $container) {
$urlGenerator = $container->get(\Yiisoft\Router\UrlGeneratorInterface::class);
$urlGenerator->setUriPrefix('/blog');
},
];
But be careful, because the SubFolder
middleware may not affect it.
https://github.com/yiisoft/router/blob/master/README.md#route-groups
Also current debugger doesn't understand the SubFolder
prefix.
Also assets
package has different approach to configure base path
https://github.com/yiisoft/router/blob/master/README.md#route-groups
It's not a group. It's a base url.
In router url generator already support this via setUriPrefix()
method. The problem is in yiisoft/assets
package that does not take it into account.
Seems, adding router dependency to assets package is a bad idea. Maybe we can resolve this problem via configuration of packages or application templates.
Worth a look. I'm checking...