yiisoft/yii2-twig

url() and path() seem to be broken

Kolyunya opened this issue · 6 comments

    public function path($path, $args = [])
    {
        if ($args !== []) {
            $path = array_merge([$path], $args);
        }
        return Url::to($path);
    }

    public function url($path, $args = [])
    {
        if ($args !== []) {
            $path = array_merge([$path], $args);
        }
        return Url::to($path, true);
    }

Both functions pass paths to Url::to as a strings when no arguments are provided. This results in original paths being returned instead of URLs being created by Url::to.

@samdark thanks!

#46 intends to maintain BC but makes function behavior bit inconsistent:

  • When we want to create a URL from path with parameters we can pass it (path) as a string like this: url('foo/bar', {baz: quux}).
  • But when we want to create a URL from path without parameters we must wrap it in an array: url(['foo/bar']).

What could be the use-case of passing a regular string to Url::to without parameters?

What could be the use-case of passing a regular string to Url::to without parameters?

Quoting docs:

  • a string with a leading @: it is treated as an alias, and the corresponding aliased string
    will be returned.
  • an empty string: the currently requested URL will be returned;
  • a normal string: it will be returned as is.

@samdark thanks. I missed that point. Hope we can fix this issue soon.

So do you think #46 is OK?

@samdark yes, I think it is impossible to create a better wrapper around Url::to without functionality loss.