我一直在寻找解决方案,但找不到任何可以解决我的问题的答案。我有最简单的表单登录(仍在学习 symfony2 ),但我不知道为什么它不起作用。
我的安全.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:
secured_area:
pattern: ^/profile
provider: in_memory
form_login:
check_path: _security_check
login_path: _portal_login
username_parameter: _username
password_parameter: _password
access_control:
- { path: ^/profile, roles: ROLE_ADMIN }
我的默认控制器:
class DefaultController extends Controller
{
/**
* @Route("/login", name="_portal_login")
* @Template()
*/
public function indexAction(Request $request)
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
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 array(
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error
);
}
/**
* @Route("/login_check", name="security_check")
*/
public function securityCheckAction()
{
}
/**
* @Route("/profile", name="profile_main")
* @Template()
*/
public function profileAction()
{
return array();
}
}
并查看我的登录表单
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path("security_check") }}" method="post" id="login">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<button type="submit">login</button>
</form>
正如我所说 - 尽可能简单,点击“登录”后我得到异常:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
编辑 !!!!
刚刚找到一个解决方案:您需要编辑 security.yml 以便提供程序位于 form_login 部分 ^^