【发布时间】:2019-02-22 15:12:05
【问题描述】:
是否可以在 Twig 中生成包含用户名和密码的绝对 URL?:
http://foo:bar@example.com/
【问题讨论】:
是否可以在 Twig 中生成包含用户名和密码的绝对 URL?:
http://foo:bar@example.com/
【问题讨论】:
我建议将此作为 absolute_url() 的功能,但被拒绝:https://github.com/symfony/symfony/issues/30281
所以这是我想出的自制解决方案:创建以下Custom Twig Filter:
public function addUsername(string $url, string $username, string $password)
{
$delimiter = '://';
$parts = explode($delimiter, $url);
return $parts[0].$delimiter.$username.':'.$password.'@'.$parts[1];
}
并像这样使用它:
{{ absolute_url(path('route-name'))|addUsername('foo', 'bar') }}
【讨论】: