2014-03-02 64 views
1

我試圖在Symfony2的這個配置配置驗證:配置認證的Symfony2

Security.yml

security: 
    encoders: 
     Symfony\Component\Security\Core\User\User: plaintext 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] 

    providers: 
     in_memory: 
      memory: 
       users: 
        user: { password: userpass, roles: [ 'ROLE_USER' ] } 
        admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] } 

    firewalls: 
     admin_area: 
      pattern: ^/admin 
      provider: in_memory 
      anonymous: ~ 
      form_login: 
       login_path: login 
       check_path: login_check 
      logout: 
       path: /logout 
       target:/ 

    access_control: 
     - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin, roles: ROLE_ADMIN } 

/src/MyBundle/Resources/Routing.yml

ies_cierva_encuesta_backend_admin: 
    pattern: /admin 
    defaults: { _controller: Bundle:Default:admin } 

login: 
    pattern: /login 
    defaults: { _controller: Bundle:Login:login } 

login_check: 
    pattern: /login_check 

logout: 
    pattern: /logout 

src/Bundle/Controller/LoginController.php

<?php 

namespace ... 

use ... 

class LoginController extends Controller { 

    public function loginAction(Request $request) { 
     $session = $request->getSession(); 

     if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
      $error = $request->attributes->get(
       SecurityContext::AUTHENTICATION_ERROR 
      ); 
     } else { 
      $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); 
      $session->remove(SecurityContext::AUTHENTICATION_ERROR); 
     } 

     return $this->render(
      'Bundle:Security:login.html.twig', 
      array(
       // last username entered by the user 
       'last_username' => $session->get(SecurityContext::LAST_USERNAME), 
       'error'   => $error, 
      ) 
     ); 
    } 
} 

我收到此錯誤:

「無法找到路徑控制器」/ login_check「。也許你忘了添加在你的路由配置匹配的路線?」

如果我沒看錯,這條路線並不需要一個控制器...

回答

1

http://symfony.com/doc/current/book/security.html,它被提及

」請確保您的check_path URL(例如/ login_check)位於您用於表單登錄的防火牆後面。「

但/ login_check不在您用於表單登錄的防火牆後面。

firewalls: 
    admin_area: 
     pattern: ^/admin 
     provider: in_memory 
     anonymous: ~ 
     form_login: 
      login_path: login 
      check_path: login_check 
     logout: 
      path: /logout 
      target:/ 

在上述配置中,模式路徑「login_check」與「^/admin」模式不匹配。相應地改變模式,使其工作。