没有路径的Symfony 2路由,有可能吗?

Probably this is a simple question but I am still an amateur.

How do I create a route without a "pattern" attribute?

I mean a route corresponding to an action that isn't accessible?

In yaml if I try something like:

security_loginfailure:
  requirements:
      _controller: SecurityBundle:Security:loginFailure

Symfony tells me that my route must have a pattern. So I have to add something like

pattern: /loginfailure

But obviously a browser can request /loginfailure.

Should I configure my controller as a service? Or what?

You cannot make a route without pattern. If I understand correctly, you might want to use the forwarding:

http://symfony.com/doc/current/book/controller.html#forwarding

Ok so it is true that it doesn't make any sense. I just wanted to handle the login failure my way, and the only option I did know was the failure_path option in security yml, which, of course, accepte only a path (or a route).

What I had to do is to override the default symfony failure_handler in firewall config.

If anyone is interested I'll put this there for future reference.

Instead of

failure_path: #something

use

failure_handler: failure_service

then define your service

failure_service:
     class: failure_class
     argumets:
        - @router
        - login_route #or another route
       #- other arguments like options

failure class

use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\RedirectResponse;

class AuthenticationFailure extends DefaultAuthenticationFailureHandler
{
    protected $router;
    protected $route;
    protected $options = array();

    public function __construct(RouterInterface $router, $route, array $options) 
    {
          $this->router = $router;
          $this->route = $route;
          $this->options = $options;
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        //finally we can do something
        return new RedirectResponse($this->router->generate($this->route));
    }
}

This may seem quite the same thing, but in reality it allows you to customize in depth the auth failure process.