解決策を探していましたが、問題を解決する答えが見つかりませんでした。私は最も単純なフォーム ログイン (まだ 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:
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 }
私のDefaultController :
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 セクションにあるようにする必要があります ^^