好了,我们快完成了,真的!
创建模板
创建login.html.twig文件(目录代码中可见):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{# src/Yoda/UserBundle/Resources/views/Security/login.html.twig #} {% if error %} <div>{{ error.message }}</div> {% endif %} <form action="{{ path('login_check') }}" method="post"> <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" /> {# If you want to control the URL the user is redirected to on success (more details below) <input type="hidden" name="_target_path" value="/account" /> #} <button type="submit">login</button> </form> |
以上_username 和 _password 字段缺一不可,否则symfony无法取到值会输出一个login错误信息。当我们提交数据时,symfony会寻找这些字段,所以他们很重要。
提示:你可以改变这些字段的名称,你可以google 搜索username_parameter and password_parameter options.
让我们继承base.html.twig模板,添加一个小元素:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
{# src/Yoda/UserBundle/Resources/views/Security/login.html.twig #} {% extends '::base.html.twig' %} {% block body %} <section class="login"> <article> {% if error %} <div class="error">{{ error.message }}</div> {% endif %} <form action="{{ path('login_check') }}" method="post"> <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> </article> </section> {% endblock %} |
处理登录:login_check
是检查路由!还是控制器!检查模板!让我们试一试,一个错误提示:
Unable to generate a URL for the named route “login_check” as such route does not exist.
是检查路由,我们在模板中的表单里有一个action路径为login_check。所以我们要创建一个方法并且给他指定@Route为login_check:
1 2 3 4 5 6 7 8 9 |
// ... // src/Yoda/UserBundle/Controller/SecurityController.php /** * @Route("/login_check", name="login_check") */ public function loginCheckAction() { } |
我快疯了,不知道怎么办了。通常情况下,这意味着,你访问/login_check将执行控制器,因为没有返回任何东西,应该会爆出一个错误。说明我们的login_check还是普通的路由并没有和Security结合。
配置login_path和check_path
我们现在打开Security.yml并找到form_login配置:
1 2 3 4 5 6 7 8 9 10 |
# app/config/security.yml # ... firewalls: secured_area: pattern: ^/ form_login: check_path: _security_check login_path: /my-login-url # ... |
你应该更改login_path为/login ,当用户没有角色时,他会被重定向到登录页面
你还应该更改check_path为/login_check ,当用户提交时Security会判断它填写的登录信息是否正确。
现在你去访问 /new.我们可以看到页面重定向到 /login 页面,感谢login_path的关键配置。
使用和了解登录过程
现在,让我告诉你symfony安全系统的牛逼之一。当我们使用user和userpass登录时,他工作了!我们能看到username在web debug工具中并且还分配了一个角色。到底发生了什么事情?
但我们提交,symfony安全系统拦截了这个请求,并且处理了登录信息。他是这样工作的只要你使用post方式提交_username和_password到/login_check地址。这个URL时非常特殊的,因为该路由被配置到Security.yml的check_path键上。它就永远也不会执行loginCheckAction方法,因为symfony拦截了他的post请求。
那我们的user和userpass从哪里来的?其实就是Security.yml里的默认用户:
1 2 3 4 5 6 7 8 9 |
# app/config/security.yml # ... providers: in_memory: memory: # this was here when we started: 2 hardcoded users users: user: { password: userpass, roles: [ 'ROLE_USER' ] } admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] } |
我们可以再一分钟内,将用户加载到数据库。
非常感谢楼主无私奉献资料。小弟有个不情之请。可以把文章分页吗?比如说1,2,3,4之类的,这样方便查找想看的内容。3Q
用处很大阿,楼主加油!
为什么写了provider 使用User跟admin登录都是提示Bad Credentials