2016-11-21 43 views
4

我无法弄清楚,为什么我无法登录。Symfony2,无法登录,也许原因是翻译?

我得到错误“控制器必须返回一个响应(null给出)。你忘了在你的控制器的某个地方添加一个return语句吗?”

(和打印从SecurityController一条消息,如果我去掉,loginCheckAction(路线sec_l​​ogin_check),其据我明白不应被打印(=执行)。

我已删除翻译,除去任何前缀(像从登录路由安全性),并创建了一个新的项目。尽管如此,我仍然得到这个错误。

// C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym\demo\src\Demo\DemoBundle\Controller\SecurityController.php 

<?php 

namespace Demo\DemoBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; 


use Demo\DemoBundle\Entity\User; 

/** 
* Class SecurityController 
* 
* @Route("/security") 
* @package Demo\DemoBundle\\Controller 
*/ 
class SecurityController extends Controller { 
    /** 
    * @Route("/login", name="sec_login_form") 
    * @Template("DemoDemoBundle:Security:login.html.twig") 
    * @Method("GET|POST") 
    */ 
    public function loginAction() 
    { 

    $auth_checker = $this->get('security.authorization_checker'); 

    if ($auth_checker->isGranted('IS_AUTHENTICATED_ANONYMOUSLY') 
     && !($auth_checker->isGranted('ROLE_USER') || $auth_checker->isGranted('ROLE_ADMIN') ) 
      ) { 

     $authenticationUtils = $this->get('security.authentication_utils'); 
     $error = $authenticationUtils->getLastAuthenticationError(); 
     $lastUsername = $authenticationUtils->getLastUsername(); 

     return $this->render (
       'DemoDemoBundle:Security:login.html.twig' , 
      array(
      'last_username' => $lastUsername, 
      'error'   => $error, 
     )); 
    } 
    else { 
     return $this->render(
       'DemoDemoBundle:Default:index.html.twig', 
       array('msg' => "You are already logged in!",)); //case of bundle 
     } 
    } 

    /** 
    * @Route("/login_check", name="sec_login_check") 
    */ 
    public function loginCheckAction() { 
     //print_r(" there is something wrong with the firewall if you see this :loginCheckAction, route name sec_login_check"); 
} 

    /** 
    * @Route("/logout", name="sec_logout") 
    */ 
    public function logoutAction() {} 

} 

// C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym\demo\app\config\security.yml security:

encoders: 
     Demo\DemoBundle\Entity\User: sha512 

    providers: 
     database_users: 
      entity: 
       class: DemoDemoBundle:User 
       property: username 

    role_hierarchy: 
     ROLE_AUTHOR:  [ROLE_USER] 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     secured_area: 
      pattern: ^/ , but it gives the same error 
      #also used pattern: ^/security/ but it gives the same error 
      provider: database_users 
      anonymous: ~ 
      access_denied_url: /security/login 
      form_login: 
       check_path: sec_login_check 
       login_path: sec_login_form 
      logout: 
       path: sec_logout 
       target: event_index 

     access_control: 
#i have used only one case from below at a time: 

#case 1 gives the error: The controller must return a response (null given) 
      - { path: ^/security/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/, roles: ROLE_USER } 

#case 2 gives the error: The controller must return a response (null given) 
      - { path: ^/security/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/, roles: ROLE_USER } 

#case 3 gives the error: The controller must return a response (null given) 
      - { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY } 

// C:\Bitnami\wampstack-5.6.20-0\apache2\htdocs\sym\demo\app\config\routing.yml

demo_demo_security: 
    resource: "@DemoDemoBundle/Controller/SecurityController.php" 
    type:  annotation 
    prefix: /

sec_login_form: 
    path: /security/login 
    defaults: { _controller: 'DemoDemoBundle:Security:login' } 
    options: { compiler_class: Symfony\Component\Routing\RouteCompiler } 
    methods: [POST, GET] 

sec_login_check: 
    path: /security/login_check 
    defaults: { _controller: 'DemoDemoBundle:Security:loginCheck' } 
    options: { compiler_class: Symfony\Component\Routing\RouteCompiler } 
    methods: [POST, GET] 

sec_logout: 
    path: /security/logout 
    defaults: { _controller: 'DemoDemoBundle:Security:logout' } 
    options: { compiler_class: Symfony\Component\Routing\RouteCompiler } 

****************这是个问题我也问过前两个星期。

(我从路由中删除了翻译配置和{_trans},但是仍然发现上面的错误在开始时我徘徊了,也许原因是翻译器?如何配置登录,如果我在树枝中使用|trans 。模板

sym\just2\app\config\routing.yml 

    sec_login_form: 
     path: /security/login 
     defaults: { _controller: 'MeetingBundle:Security:login' } 
     options: { compiler_class: Symfony\Component\Routing\RouteCompiler } 
     methods: [POST, GET] 

    sec_login_check: 
     path: /security/login_check 
     defaults: { _controller: 'MeetingBundle:Security:loginCheck' } 
     options: { compiler_class: Symfony\Component\Routing\RouteCompiler } 
     methods: [POST, GET] 
    sec_logout: 
     path: /security/logout 
     defaults: { _controller: 'MeetingBundle:Security:logout' } 
     options: { compiler_class: Symfony\Component\Routing\RouteCompiler } 

C:\ Bitnami \ wampstack-5.6.20-0 \ Apache2的\ htdocs中\符号\ just2 \程序\ CONFIG \ security.yml

security: 

     encoders: 
      MeetingBundle\Entity\User: sha512 

     providers: 
      database_users: 
       entity: { class: MeetingBundle:User } 

     role_hierarchy: 
      ROLE_AUTHOR:  [ROLE_USER] 
      ROLE_ADMIN:  [ROLE_ADMIN] 

     firewalls: 
      dev: 
       pattern: ^/(_(profiler|wdt)|css|images|js)/ 
       security: false 

      secured_area: 
       pattern: ^/ 
       provider: database_users 
       anonymous: true 
       access_denied_url: /security/login 
       form_login: 
        check_path: /security/login_check 
# did not work either: sec_login_check 
        login_path: /security/login 
# did not work either: sec_login_form 
        default_target_path: event_index 
        always_use_default_target_path: true 
       logout: 
        path: /security/logout 
# did not work either: sec_logout 
        target: event_index 

     access_control: 
      - { path: ^/security/login_check$, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/security/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/event/, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/evcom/, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/security/regist1$, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/security/remind$, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/security/confirm$, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
      - { path: ^/security/reset$, roles: [ROLE_USER] } 
      - { path: ^/security/logout$, roles: [ROLE_USER]} 
      - { path: ^/eventjoin, roles: [ROLE_USER] } 
      - { path: ^/user/, roles: [ROLE_USER] } 
      - { path: ^/event/.+, roles: [ROLE_USER] } 
      - { path: ^/message/.+, roles: [ROLE_USER] }   
      - { path: ^/, roles: [ROLE_USER, ROLE_ADMIN, IS_AUTHENTICATED_ANONYMOUSLY] } 
      } 

C:\ Bitnami \ wampstack -5.6.20-0 \ apache2的\ htdocs中\符号\ just2 \ SRC \ MeetingBundle \控制器\ SecurityController。 PHP

<?php 

    namespace MeetingBundle\Controller; 

    use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 
    // use Symfony\Component\HttpFoundation\Response; 


    use MeetingBundle\Entity\User; 

    /** 
    * Class SecurityController 
    * 
    * @Route("/security") 
    * @package MeetingBundle\Controller 
    */ 
    class SecurityController extends Controller { 

    /* EXPLANATIONS about security 
    * http://www.reecefowell.com/2012/04/30/authorisation-on-roles-in-symfony2/ 
    */ 

     /** 
     * @Route("/login", name="sec_login_form") 
     * @Template("MeetingBundle:Security:login.html.twig") 
     * @Method("GET|POST") 
     */ 
     public function loginAction() 
     { 

     $auth_checker = $this->get('security.authorization_checker'); 

     if ($auth_checker->isGranted('IS_AUTHENTICATED_ANONYMOUSLY') 
      && !($auth_checker->isGranted('ROLE_USER') || $auth_checker->isGranted('ROLE_ADMIN') ) 
       ) { 
      $authenticationUtils = $this->get('security.authentication_utils'); 
      $error = $authenticationUtils->getLastAuthenticationError(); 
      $lastUsername = $authenticationUtils->getLastUsername(); 

      return $this->render (
        'MeetingBundle:Security:login.html.twig' , 
       array(
       'last_username' => $lastUsername, 
       'error'   => $error, 
      )); 

     } 
     else { 
      return $this->render(
        'MeetingBundle:Security:msg.html.twig', 
        array('msg' => "You are already logged in!",)); //case of bundle 
      } 

     } 

     /** 
     * @Route("/login_check", name="sec_login_check") 
     * 
     */ 
     public function loginCheckAction() 
     { 
      print_r(" there is something wrong with the firewall if you see this :loginCheckAction, route name sec_login_check"); 
      // return new Response();- returns, there is an empty page 
     } 

     /** 
     * @Route("/logout", name="sec_logout") 
     */ 
     public function logoutAction() 
     { 
     } 

    } 

C:\ Bitnami \ wampstack-5.6.20-0 \ Apache2的\ htdocs中\符号\ just2的\ src \ MeetingBundle \资源\意见\安全\ login.html.twig

{% extends "MeetingBundle::layoutBare.html.twig" %} 

{% block stylesheets %} {{ parent() }} {% endblock %} 
{% block header %} {{ parent() }} {% endblock %} 
{% block msg %} {{ parent() }} {% endblock %} 

{% block body %} 

    <form action="{{ path('sec_login_check') }}" method="get" class="form" id="Security_login"> 

     <hr><span class="desc-large-left" > Username: </span> 
     <hr><input class="topcoat-text-input" type="text" id="username" name="_username" /> 

     <hr><span class="desc-large-left" > Password: </span> 
     <hr><input class="topcoat-text-input" type="password" id="password" name="_password"/> 
     <hr><button class="topcoat-button large" type="submit"> {{ 'sec.login'|trans}} </button> 

    </form> 

    {% block javascripts %} {{ parent() }} {% endblock %} 
{% endblock %} 
+0

什么错误,你有吗? – DOZ

+0

检查数据库的连接。 – CStff

+0

错误是'控制器必须返回一个响应(null给出)。你忘了在控制器的某个地方添加一个return语句吗?“在过去的两周里,我忙于其他事情。现在回来登录,删除翻译,但登录不起作用。 – olga

回答

0

我认为这个问题是在loginCheckAction你应该返回的东西不能打印或与该测试响应:

public function loginCheckAction(){ 
    throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.'); 
} 
+0

欢迎来到SO!请使用英文,观众可能不知道这种语言:) – mrid