简体   繁体   中英

Unable to generate a URL for the named route “login”

I'm getting error Unable to generate a URL for the named route "login" as such route does not exist. when I start the server and try to access http://127.0.0.1:8000 .

Direct access to /login gives: No route found for "GET /login"

This is with symfony3 and WITHOUT FOSBUNDLE! Based on 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,
  )
);
 }
}

I guess you are missing the routing.yml file. In the config.yml file you need to define the routing part:

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

the routing.yml file needs to look like then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM