Allow trailing slash for Symfony2 route without params?

前端 未结 7 1171
时光取名叫无心
时光取名叫无心 2020-12-08 19:08
acme_admin_dashboard:
    pattern:  /{_locale}/admin
    defaults: { _controller: AcmeBundle:Admin:dashboard }

I want this route to be accessible a

7条回答
  •  隐瞒了意图╮
    2020-12-08 19:29

    For new SF versions:

    By default, the Symfony Routing component requires that the parameters match the following regex path: [^/]+. This means that all characters are allowed except /.

    You must explicitly allow / to be part of your parameter by specifying a more permissive regex path.

    YAML:

    _hello:
        path:     /hello/{username}
        defaults: { _controller: AppBundle:Demo:hello }
        requirements:
            username: .+
    

    XML:

    
    
        
            AppBundle:Demo:hello
            .+
        
    
    

    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;
    

    Annotations:

    /**
     * @Route("/hello/{username}", name="_hello", requirements={"username"=".+"})
     */
    public function helloAction($username)
    {
        // ...
    }
    

提交回复
热议问题