acme_admin_dashboard:
pattern: /{_locale}/admin
defaults: { _controller: AcmeBundle:Admin:dashboard }
我希望可以在/en/admin
和/en/admin/
访问此路线。我怎么做到这一点?
答案 0 :(得分:35)
我喜欢@ Kuchengeschmack的回答(https://stackoverflow.com/a/11078348/593957),因为它不会触发外部重定向。
这是一个yaml版本:
acme_admin_dashboard:
pattern: /{_locale}/admin{trailingSlash}
defaults: { _controller: AcmeBundle:Admin:dashboard, trailingSlash : "/" }
requirements: { trailingSlash : "[/]{0,1}" }
答案 1 :(得分:20)
只需输入:
/**
* @Route("/route/of/some/page/")
*/
所以
www.example.com/route/of/some/page
和
www.example.com/route/of/some/page/
被接受......
答案 2 :(得分:10)
您也可以在.htaccess文件中使用重写规则:
假设您已经定义了这样的路线:
news:
url: /news
param: { module: news, action: index }
这将与http://something.something/news匹配,但不会由http://something.something/news/匹配 您可以添加带有斜杠的其他路径,但您也可以在.htaccess文件中使用此重写规则:
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
http://symfony-blog.driebit.nl/2010/07/url-routes-with-or-without-a-trailing-slash/
答案 3 :(得分:10)
我找到了一个向路径添加尾部斜杠的解决方案。
表示这两个链接均正常www.example.com/route/of/some/page
和www.example.com/route/of/some/page/
。你可以这样做:
如果您的路线看起来像
/**
* @Route("/route/of/some/page")
*/
public function pageAction() {
将ist改为
/**
* @Route("/route/of/some/page{trailingSlash}", requirements={"trailingSlash" = "[/]{0,1}"}, defaults={"trailingSlash" = "/"})
*/
public function pageAction() {
答案 4 :(得分:6)
我将以下一行入侵前控制器(app.php / app_dev.php)
$_SERVER['REQUEST_URI'] = preg_replace('|/$|', '', $_SERVER['REQUEST_URI'], 1);
之前$request = Request::createFromGlobals()
答案 5 :(得分:5)
路线:
remove_trailing_slash:
path: /{url}
defaults: { _controller: AppBundle:Redirecting:removeTrailingSlash }
requirements:
url: .*/$
methods: [GET]
<强>控制器:强>
// src/AppBundle/Controller/RedirectingController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class RedirectingController extends Controller
{
public function removeTrailingSlashAction(Request $request)
{
$pathInfo = $request->getPathInfo();
$requestUri = $request->getRequestUri();
$url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri);
return $this->redirect($url, 301);
}
}
了解详情:http://symfony.com/doc/current/routing/redirect_trailing_slash.html
答案 6 :(得分:1)
对于新的SF版本:
默认情况下,Symfony Routing组件要求参数与以下正则表达式路径匹配:[^/]+
。这意味着除了/
之外,所有字符都被允许。
您必须通过指定更宽松的正则路径路径,明确允许/
成为参数的一部分。
<强> YAML:强>
_hello:
path: /hello/{username}
defaults: { _controller: AppBundle:Demo:hello }
requirements:
username: .+
<强> XML:强>
<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>
<强> PHP:强>
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;
<强>注解强>
/**
* @Route("/hello/{username}", name="_hello", requirements={"username"=".+"})
*/
public function helloAction($username)
{
// ...
}