[英]Simple login form in Symfony2
I've been looking for solution but couldn't find any answer which would fix my problem. 我一直在寻找解决方案,但找不到任何可以解决我问题的答案。 I have the simpliest form login ( still learning symfony2 ) and I have no idea why it's not working. 我的表单登录方式最简单(仍在学习symfony2),我也不知道为什么它不起作用。
My security.yml 我的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 }
My DefaultController : 我的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();
}
}
and view of my login form 并查看我的登录表单
{% 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>
As I said - as simple as it can be, and after clicking "login" I get exception : 如我所说-尽可能简单,单击“登录”后出现异常 :
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
EDIT !!!! 编辑!!!!
JUST FOUND A SOLUTION: You need to edit security.yml so provider is in form_login section ^^ 刚找到一个解决方案:您需要编辑security.yml,以便提供程序位于form_login部分^^中。
try this code in your controller 在您的控制器中尝试此代码
/**
* @Route("/login", name="_success_login")
* @Template()
*/
public function LoginAction()
{
//your code
return $this->render('BundleName:Security:login.html.twig');
}
IN security.yml 在security.yml中
form_login:
check_path: _security_check
login_path: _success_login
Please look the page click Here 请看页面点击这里
You have specified that only things that live under "/profile" should be firewalled. 您已指定仅对位于“ / profile”下的内容进行防火墙保护。 Change the pattern to: 将模式更改为:
secured_area:
pattern: ^/
form_login:
check_path: /login_check
login_path: /login
That will require authentication and authorization for your entire app. 这将需要整个应用程序的身份验证和授权。 Or 要么
secured_area:
pattern: ^/Admin
form_login:
check_path: /login_check
login_path: /login
That will require authentication and authorization only for routes that live under /Admin. 这仅需要对/ Admin下的路由进行身份验证和授权。
You didn't post your routing.yml file, but you will probably need to change your routing.yml file to this: 您没有发布routing.yml文件,但是您可能需要将routing.yml文件更改为:
login:
pattern: /login
defaults: { _controller: SomeBundle:Authentication:logon }
login_check:
pattern: /login_check
logout:
pattern: /logout
Then I would rename your DefaultController to Authentication, since that is what its really doing. 然后,我会将您的DefaultController重命名为Authentication,因为这是它的真正功能。
Read how to use Template annotation here . 在此处阅读如何使用模板注释。 Or change your return statement to this: 或将您的return语句更改为此:
return $this->render('bundle:controller:template', array(
'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error
));
Read about Template Naming and Locations here 在此处阅读有关模板命名和位置的信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.