我無法弄清楚,爲什麼我無法登錄。Symfony2,無法登錄,也許原因是翻譯?
我得到錯誤「控制器必須返回一個響應(null給出)。你忘了在你的控制器的某個地方添加一個return語句嗎?」
(和打印從SecurityController一條消息,如果我去掉,loginCheckAction(路線sec_login_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 %}
什麼錯誤,你有嗎? – DOZ
檢查數據庫的連接。 – CStff
錯誤是'控制器必須返回一個響應(null給出)。你忘了在控制器的某個地方添加一個return語句嗎?「在過去的兩週裏,我忙於其他事情。現在回來登錄,刪除翻譯,但登錄不起作用。 – olga