記錄踩過的坑,以後的坑就會越來越少…
首先建表:
<code><span>php</span><span>artisan</span><span>migrate</span><span>:make_admin_table</span></code>
然後在新建的檔案裡面寫上表的各列設定
<code><span><?</span>php use Illuminate<span>\</span>Database<span>\</span>Schema<span>\</span>Blueprint; use Illuminate<span>\</span>Database<span>\</span>Migrations<span>\</span>Migration; class CreateAdminTable extends Migration { <span>/** * Run the migrations. * * @return void */</span><span>public</span> function up() { Schema<span>::create</span>(<span>'admin'</span>, function(<span>$table</span>) { <span>$table</span><span>-></span>increments(<span>'id'</span>); <span>$table</span><span>-></span><span>string</span>(<span>'staff_code'</span>, <span>32</span>)<span>-></span>nullable(); <span>//员工号</span><span>$table</span><span>-></span><span>string</span>(<span>'login_name'</span>, <span>32</span>)<span>-></span>nullable(); <span>//登录名</span><span>$table</span><span>-></span><span>string</span>(<span>'password'</span>, <span>32</span>)<span>-></span>nullabele(); <span>//登录密码</span><span>$table</span><span>-></span><span>string</span>(<span>'mail'</span>, <span>512</span>)<span>-></span>nullable(); <span>//电子邮箱</span><span>$table</span><span>-></span><span>string</span>(<span>'staff_name'</span>, <span>32</span>)<span>-></span>nullable(); <span>//员工姓名</span><span>$table</span><span>-></span><span>string</span>(<span>'sex'</span>, <span>10</span>)<span>-></span>nullable(); <span>//性别</span><span>$table</span><span>-></span><span>string</span>(<span>'belong_to'</span>, <span>512</span>)<span>-></span>nullable(); <span>//所属部门</span><span>$table</span><span>-></span><span>string</span>(<span>'jobs'</span>, <span>512</span>)<span>-></span>nullable(); <span>//岗位</span><span>$table</span><span>-></span><span>string</span>(<span>'telephone'</span>, <span>32</span>)<span>-></span>nullable(); <span>//固定电话</span><span>$table</span><span>-></span><span>string</span>(<span>'mobile'</span>, <span>32</span>)<span>-></span>nullable(); <span>//手机号</span> }); } <span>/** * Reverse the migrations. * * @return void */</span><span>public</span> function down() { Schema<span>::dropIfExists</span>(<span>'admin'</span>); } }</code>
建立Model模型Admin:
<code>php ratisan <span>generate</span> modle Admin</code>
再在生成的文件裡添加
<code><span><?php</span><span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserTrait</span>; <span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserInterface</span>; <span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableTrait</span>; <span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableInterface</span>; <span><span>class</span><span>Admin</span><span>extends</span> \<span>Eloquent</span><span>implements</span><span>UserInterface</span>, <span>RemindableInterface</span> {</span><span>use</span><span>UserTrait</span>, <span>RemindableTrait</span>; <span>protected</span><span>$fillable</span> = []; <span>protected</span><span>$table</span> = <span>'admin'</span>; <span>// 指定表名</span><span>protected</span><span>$primaryKey</span> = <span>'id'</span>; <span>// 指定主键名</span><span>protected</span><span>$hidden</span> = <span>array</span>(<span>'password'</span>); <span>//密码字段</span><span>public</span><span>$timestamps</span> = <span>false</span>; <span>// 关闭 创建时间 与 更新时间 的自动维护</span><span>public</span><span><span>function</span><span>getRememberToken</span><span>()</span>{</span><span>return</span><span>$this</span>->rememberToken ; } <span>public</span><span><span>function</span><span>setRememberToken</span><span>(<span>$value</span>)</span>{</span><span>$this</span>->rememberToken = <span>$value</span> ; } <span>public</span><span><span>function</span><span>getRememberTokenName</span><span>()</span>{</span><span>return</span><span>$this</span>->reminder ; } }</span></code>
解釋一下,因為需要做登錄驗證,用的是laravel自帶的Auth所以需要添加use和繼承UserInterface以及RemindableInterface接口並重寫一些方法
具體的就是這幾句
<code><span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserTrait</span>; <span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserInterface</span>; <span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableTrait</span>; <span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableInterface</span>; <span><span>class</span><span>Admin</span><span>extends</span> \<span>Eloquent</span><span>implements</span><span>UserInterface</span>, <span>RemindableInterface</span> {</span><span>use</span><span>UserTrait</span>, <span>RemindableTrait</span>; <span>/******* 以下代码省略 *******/</span><span>public</span><span><span>function</span><span>getRememberToken</span><span>()</span>{</span><span>return</span><span>$this</span>->rememberToken ; } <span>public</span><span><span>function</span><span>setRememberToken</span><span>(<span>$value</span>)</span>{</span><span>$this</span>->rememberToken = <span>$value</span> ; } <span>public</span><span><span>function</span><span>getRememberTokenName</span><span>()</span>{</span><span>return</span><span>$this</span>->reminder ; } }</code>
然後我繼續找到Auth檔案的設定 修改一下需要用到的表
app/config/auth.php
找到以下欄位並修改成自己所指定的表
<code><span><?php</span><span>return</span><span>array</span>( <span>'driver'</span> => <span>'eloquent'</span>, <span>//验证方式,有database和eloquent两种</span><span>'model'</span> => <span>'Admin'</span>, <span>//所使用的model名</span><span>'table'</span> => <span>'admin'</span>, <span>//对应的表名</span><span>'reminder'</span> => <span>array</span>( <span>'email'</span> => <span>'emails.auth.reminder'</span>, <span>'table'</span> => <span>'password_reminders'</span>, <span>'expire'</span> => <span>60</span>, ), );</span></code>
然後再加入controller方法:
<code><span>//获取登录页面</span><span>public</span> function get_web_login(){ <span>return</span> View<span>::make</span>(<span>'web.web_login'</span>); } <span>//登录验证</span><span>public</span> function post_login(){ <span>if</span> (Auth<span>::attempt</span>(<span>array</span>(<span>'login_name'</span><span>=></span>Input<span>::get</span>(<span>'login_name'</span>), <span>'password'</span><span>=></span>Input<span>::get</span>(<span>'password'</span>)))) { Notification<span>::success</span>(<span>'登录成功'</span>); <span>return</span> Redirect<span>::to</span>(<span>'/web/index'</span>) <span>-></span><span>with</span>(<span>'message'</span>, <span>'成功登录'</span>); } <span>else</span> { Notification<span>::warning</span>(<span>'用户名密码不正确'</span>); <span>return</span> Redirect<span>::to</span>(<span>'/web/login'</span>) <span>-></span><span>with</span>(<span>'message'</span>, <span>'用户名密码不正确'</span>) <span>-></span>withInput(); } }</code>
然後是視圖檔login.blade.php:
<code><span>@section</span>(<span>'title'</span>)登录 - <span>@parent</span><span>@stop</span><span>@section</span>(<span>'nav_1'</span>) <li <span>class</span>=<span>"active"</span>><a href=<span>"#"</span>>登录</a></li> <span>@stop</span><span>@section</span>(<span>'selection'</span>) <div id=<span>"login"</span><span>class</span>=<span>"login"</span>> <form <span>class</span>=<span>"form"</span> role=<span>"form"</span> action=<span>"{{URL::route('web.web_login.post')}}"</span> style=<span>"width: 500px"</span> method=<span>"post"</span>> <span>@if</span> (Session::has(<span>'message'</span>)) <div <span>class</span>=<span>"alert alert-error"</span>>{{ Session::get(<span>'message'</span>)}}</div> <span>@endif</span> <div <span>class</span>=<span>"form-group"</span>> <label <span>for</span>=<span>"login_name"</span>>登录名:</label> <input <span>type</span>=<span>"text"</span><span>class</span>=<span>"form-control"</span> id=<span>"login_name"</span> name=<span>"login_name"</span>> <label <span>for</span>=<span>"password"</span>>密码:</label> <input <span>type</span>=<span>"password"</span><span>class</span>=<span>"form-control"</span> id=<span>"password"</span> name=<span>"password"</span>> </div> <div align=<span>"left"</span>> <button <span>type</span>=<span>"submit"</span><span>class</span>=<span>"btn btn-info btn-lg"</span>><span <span>class</span>=<span>"glyphicon glyphicon-user"</span> aria-hidden=<span>"true"</span>></span> 登录</button> </div> </form> </div> <span>@stop</span></code>
最後更新路由
<code>Route::get(<span>'/web/index'</span>, <span>array</span>(<span>'as'</span> => <span>'web.web_index'</span>, <span>'uses'</span> => <span>'App\Controllers\Api\WebController@get_web_index'</span>)); <span>//登录页面</span> Route::get(<span>'/web/login'</span>, <span>array</span>(<span>'as'</span> => <span>'web.web_login'</span>, <span>'uses'</span> => <span>'App\Controllers\Api\WebController@get_web_login'</span>)); Route::post(<span>'/web/login'</span>, <span>array</span>(<span>'as'</span> => <span>'web.web_login.post'</span>, <span>'uses'</span> => <span>'App\Controllers\Api\WebController@post_login'</span>)); </code>
<code><span>/** * Attempt to authenticate a user using the given credentials. * *<span> @param</span> array $credentials *<span> @param</span> bool $remember *<span> @param</span> bool $login *<span> @return</span> bool *<span> @static</span> */</span><span>public</span><span>static</span> function <span>attempt</span>($credentials = array(), $remember = <span>false</span>, $login = <span>true</span>){ <span>return</span> \Illuminate\Auth\Guard::attempt($credentials, $remember, $login); }</code>
百度google了一下,然而並沒有找到什麼結果
無奈之下只能看看laravel的源碼
<code><span>/** * Attempt to authenticate a user using the given credentials. * * @param array $credentials * @param bool $remember * @param bool $login * @return bool */</span><span>public</span> function attempt(<span>array</span><span>$credentials</span><span>=</span><span>array</span>(), <span>$remember</span><span>=</span><span>false</span>, <span>$login</span><span>=</span><span>true</span>) { <span>$this</span><span>-></span>fireAttemptEvent(<span>$credentials</span>, <span>$remember</span>, <span>$login</span>); <span>$this</span><span>-></span>lastAttempted <span>=</span><span>$user</span><span>=</span><span>$this</span><span>-></span>provider<span>-></span>retrieveByCredentials(<span>$credentials</span>); <span>// If an implementation of UserInterface was returned, we'll ask the provider</span><span>// to validate the user against the given credentials, and if they are in</span><span>// fact valid we'll log the users into the application and return true.</span><span>if</span> (<span>$this</span><span>-></span>hasValidCredentials(<span>$user</span>, <span>$credentials</span>)) { <span>if</span> (<span>$login</span>) <span>$this</span><span>-></span>login(<span>$user</span>, <span>$remember</span>); <span>return</span><span>true</span>; } <span>return</span><span>false</span>; }</code>
<code><span>/** * Determine if the user matches the credentials. * *<span> @param</span> mixed $user *<span> @param</span> array $credentials *<span> @return</span> bool */</span><span>protected</span> function <span>hasValidCredentials</span>($user, $credentials) { <span>return</span> ! is_null($user) && $<span>this</span>->provider->validateCredentials($user, $credentials); }</code>
<code><span>/** * Validate a user against the given credentials. * *<span> @param</span> \Illuminate\Auth\UserInterface $user *<span> @param</span> array $credentials *<span> @return</span> bool */</span><span>public</span> function <span>validateCredentials</span>(UserInterface $user, array $credentials);</code>
<code><span>/** * Validate a user against the given credentials. * *<span> @param</span> \Illuminate\Auth\UserInterface $user *<span> @param</span> array $credentials *<span> @return</span> bool */</span><span>public</span> function <span>validateCredentials</span>(UserInterface $user, array $credentials) { $plain = $credentials[<span>'password'</span>]; <span>return</span> $<span>this</span>->hasher->check($plain, $user->getAuthPassword()); }</code>
<code>Hash::make(<span>"<span>$passowrd</span>"</span>);</code>
然而我是直接在在資料庫中加入明文密碼的,所以顯示密碼不正確是理所當然的
rrreee
來產生對應密碼的hash字串…
然後我在用這個方法寫入密碼hash字串的時候報錯了,檢查一看,原來是當初設定的密碼欄位太短導致的,於是把密碼欄位長度改為1024個字長 問題就解決了
這個坑折騰了我一上午…記錄下來讓大家參考參考,避免再次像我一樣被坑吧_ (:з”∠)_
以上就介紹了20150720-Laravel登入驗證碰到的坑,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。
🎜