我在symfony网站上制作了登录操作教程:http://symfony.com/doc/current/cookbook/security/form_login_setup.html
当我在表单中登录时,我会收到以下错误:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
这是我的安全。yml
security:
encoders:
Symfony'Component'Security'Core'User'User: plaintext
role_hierarchy:
ROLE_USER: ROLE_USER
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
access_denied_url: no_access
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/login
security: false
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
default_target_path: home
logout:
path: /logout
target: /login
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
- { path: ^/, roles: ROLE_USER }
我的安全控制器:
class SecurityController extends Controller
{
/**
* @Route("/login", name="login_route")
* @Template()
*/
public function loginAction()
{
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render(
'TripAdminBundle:Main:login.html.twig',
array(
// last username entered by the user
'last_username' => $lastUsername,
'error' => $error,
)
);
}
/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
// this controller will not be executed,
// as the route is handled by the Security system
}
}
点击提交按钮后的重定向是login_check,但其中没有代码,因为symfony说:the route is handled by the Security system
但我明白这个错误。有人能帮我做这个吗?
直接返回数组,不使用render方法和trick文件名。返回数组(//用户最后输入的用户名'last_username'=>$lastUsername,'error'=>$error,));