router icon indicating copy to clipboard operation
router copied to clipboard

Support base prefix for url generator

Open xepozz opened this issue 1 year ago • 6 comments

I want to write an application that will live under some path, e.g. /blog So I have two ways to write my urls:

  1. Each route should contain /blog
  2. 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.

xepozz avatar Dec 20 '22 19:12 xepozz

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.

xepozz avatar Dec 20 '22 20:12 xepozz

https://github.com/yiisoft/router/blob/master/README.md#route-groups

samdark avatar Dec 20 '22 20:12 samdark

Also current debugger doesn't understand the SubFolder prefix. Also assets package has different approach to configure base path

xepozz avatar Dec 20 '22 20:12 xepozz

https://github.com/yiisoft/router/blob/master/README.md#route-groups

It's not a group. It's a base url.

xepozz avatar Dec 20 '22 20:12 xepozz

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.

vjik avatar Jan 08 '23 19:01 vjik

Worth a look. I'm checking...

rustamwin avatar Feb 23 '23 20:02 rustamwin