无法生成指定路由“登录”的URL

时间:2016-04-12 06:44:56

标签: php routing yaml symfony

启动服务器并尝试访问http://127.0.0.1:8000时收到错误Unable to generate a URL for the named route "login" as such route does not exist.

直接访问/ login会产生:No route found for "GET /login"

这是symfony3和WITHOUT FOSBUNDLE! 基于symfony3 cookbook traditional log form

security.yml:

security:

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    in_memory:
        memory: ~

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        form_login:
          login_path: login
          check_path: login
          csrf_token_generator: security.csrf.token_manager
          default_target_path: application
          always_use_default_target_path: true
        # activate different ways to authenticate

        # http_basic: ~
        # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

        # form_login: ~
        # http://symfony.com/doc/current/cookbook/security/form_login_setup.html
access_control:
  - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUYSLY }
  - { path: ^/, roles: ROLE_ADMIN }

securityController.php:

// src/AppBundle/Controller/SecurityContoller.php
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class SecurityController extends Controller
{
/**
 * @Route("/login", name="login")
 */
 public function loginAction(Request $request)
 {
  $authenticationUtils = $this->get('security.authentication_utils');

// login error, if there is one
$error = $authenticationUtils->getLastAuthenticationError();

// last username entered
$lastUsername = $authenticationUtils->getLastUsername();

return $this->render(
  'security/login.html.twig',
  array(
    'last_username' => $lastUsername,
    'error'         => $error,
  )
);
 }
}

1 个答案:

答案 0 :(得分:3)

我猜你错过了routing.yml文件。在config.yml文件中,您需要定义路由部分:

framework:
    router:
        resource: "%kernel.root_dir%/config/internal/routing.yml"
        strict_requirements: ~

routing.yml文件需要看起来像

php:
    resource: "@AppBundle/Controller/"
    type:     annotation