是否允许Symfony2路由的尾随斜杠不带参数?
我希望此路线可以在是否允许Symfony2路由的尾随斜杠不带参数?,symfony,yaml,Symfony,Yaml,我希望此路线可以在/en/admin和/en/admin/访问。如何实现这一点?您也可以简单地在.htaccess文件中使用重写规则: news: url: /news param: { module: news, action: index } 假设您已经定义了这样一条路线: acme_admin_dashboard: pattern: /{_locale}/admin defaults: { _controller: AcmeBundle:Admin:dash
/en/admin
和/en/admin/
访问。如何实现这一点?您也可以简单地在.htaccess文件中使用重写规则:
news:
url: /news
param: { module: news, action: index }
假设您已经定义了这样一条路线:
acme_admin_dashboard:
pattern: /{_locale}/admin
defaults: { _controller: AcmeBundle:Admin:dashboard }
这将由匹配,但不是由匹配
您可以添加带有尾随斜杠的附加路由,但也可以在.htaccess文件中使用此重写规则:
news:
url: /news
param: { module: news, action: index }
我找到了一个解决方案,可以在管线上添加一个尾随斜杠 表示两个链接都在工作
www.example.com/route/of/some/page
和www.example.com/route/of/some/page/
。您可以这样做:
如果你的路线看起来像
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
改为
/**
* @Route("/route/of/some/page")
*/
public function pageAction() {
仅键入:
/**
* @Route("/route/of/some/page{trailingSlash}", requirements={"trailingSlash" = "[/]{0,1}"}, defaults={"trailingSlash" = "/"})
*/
public function pageAction() {
所以
及
我喜欢@Kuchengeschmack的答案(),因为它不会触发外部重定向 以下是yaml版本:
www.example.com/route/of/some/page/
我将下面这行代码侵入了前端控制器(app.php/app_dev.php) 对于新SF版本,在$request=request::createFromGlobals()之前 默认情况下,Symfony路由组件要求参数匹配以下正则表达式路径:
[^/]+
。这意味着除了/
之外,允许使用所有字符
您必须通过指定更具权限的正则表达式路径,明确允许/
成为参数的一部分
YAML:
$_SERVER['REQUEST_URI'] = preg_replace('|/$|', '', $_SERVER['REQUEST_URI'], 1);
_hello:
path: /hello/{username}
defaults: { _controller: AppBundle:Demo:hello }
requirements:
username: .+
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="_hello" path="/hello/{username}">
<default key="_controller">AppBundle:Demo:hello</default>
<requirement key="username">.+</requirement>
</route>
</routes>
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add('_hello', new Route('/hello/{username}', array(
'_controller' => 'AppBundle:Demo:hello',
), array(
'username' => '.+',
)));
return $collection;
remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
methods: [GET]
XML:
$_SERVER['REQUEST_URI'] = preg_replace('|/$|', '', $_SERVER['REQUEST_URI'], 1);
_hello:
path: /hello/{username}
defaults: { _controller: AppBundle:Demo:hello }
requirements:
username: .+
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="_hello" path="/hello/{username}">
<default key="_controller">AppBundle:Demo:hello</default>
<requirement key="username">.+</requirement>
</route>
</routes>
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add('_hello', new Route('/hello/{username}', array(
'_controller' => 'AppBundle:Demo:hello',
), array(
'username' => '.+',
)));
return $collection;
remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
methods: [GET]
注释:
$_SERVER['REQUEST_URI'] = preg_replace('|/$|', '', $_SERVER['REQUEST_URI'], 1);
_hello:
path: /hello/{username}
defaults: { _controller: AppBundle:Demo:hello }
requirements:
username: .+
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="_hello" path="/hello/{username}">
<default key="_controller">AppBundle:Demo:hello</default>
<requirement key="username">.+</requirement>
</route>
</routes>
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add('_hello', new Route('/hello/{username}', array(
'_controller' => 'AppBundle:Demo:hello',
), array(
'username' => '.+',
)));
return $collection;
remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
methods: [GET]
路线:
/**
* @Route("/hello/{username}", name="_hello", requirements={"username"=".+"})
*/
public function helloAction($username)
{
// ...
}
控制器:
$_SERVER['REQUEST_URI'] = preg_replace('|/$|', '', $_SERVER['REQUEST_URI'], 1);
_hello:
path: /hello/{username}
defaults: { _controller: AppBundle:Demo:hello }
requirements:
username: .+
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="_hello" path="/hello/{username}">
<default key="_controller">AppBundle:Demo:hello</default>
<requirement key="username">.+</requirement>
</route>
</routes>
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add('_hello', new Route('/hello/{username}', array(
'_controller' => 'AppBundle:Demo:hello',
), array(
'username' => '.+',
)));
return $collection;
remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
methods: [GET]
阅读更多内容:完全没有意义,只需使用公认答案中的尾随斜杠即可。我喜欢它背后的创造性思维!这并非毫无意义。它适用于飞行前选项请求,不允许有重定向。很好!正如@r0ber7所提到的,它删除了301重定向:),如果你最终在所有路由上都这样做,那么它会使你的代码变得混乱——但我喜欢这个主意!如果您的路由被无法遵循301重定向的客户机使用,这将非常有用。通过这种方式,无论有无尾随斜杠,都可以得到200响应。在侧节点上,这在使用FOSRestBundle时特别有用,因为您的REST接口没有返回外部重定向。您可以简单地使用
\?
作为要求,您可以省略默认定义。我发现这个解决方案对于它所带来的小事情来说非常沉重。。。像伊恩·达米恩(ine Damien)的答案那样写规则更好,而且效果很好。我喜欢这个解决方案。将问题限制在服务器层似乎是合适的,因为根本不需要应用程序逻辑。^但这是使应用程序按预期工作所必需的。.这有点粗糙,但我喜欢。喜欢!我认为Symfony团队应该将此解决方案集成到代码库中。但是,默认情况下,所有链接都带有尾随斜杠。。这看起来有点粗糙哦,见鬼,你不想这么做!它会永久移动301,因此不会重新发送POST参数!()看到了。但是当请求为/hello/john/时,$username将为john/并且需要额外的解析。