分享9個(gè)Laravel Auth腳手架相關(guān)的小提示

發(fā)布時(shí)間:2024-04-12
下面由laravel教程欄目給大家分享9個(gè)laravel auth腳手架相關(guān)的小提示,希望對(duì)需要的朋友有所幫助!
laravel擁有一個(gè)很棒的現(xiàn)成的用戶(hù)認(rèn)證系統(tǒng),當(dāng)然我們也需要在在某些地方自定義一些配置。對(duì)于某些自定義配置,我們并不需要再去尋找一個(gè)擴(kuò)展包或者寫(xiě)一大堆代碼。讓我們來(lái)研究一下這套認(rèn)證系統(tǒng)背后隱藏著哪些有趣的功能。
技巧 1. auth::routes() 參數(shù)
我們應(yīng)該都知道方法 auth::routes() 來(lái)自于 laravel ui package (在laravel 7之前, 它被包含在內(nèi)核中)。
但你知道它可以接受一個(gè)數(shù)組來(lái)啟用/禁用特定的認(rèn)證路由嗎?
對(duì)于laravel 7,下面是可用的參數(shù)及其默認(rèn)值:
auth::routes([ 'login' => true, 'logout' => true, 'register' => true, 'reset' => true, // 用于重置密碼 'confirm' => false, // 用于額外的密碼確認(rèn) 'verify' => false, // 用于郵箱認(rèn)證]);這些參數(shù)僅啟用或禁用某些路由。
要了解它們是如何工作的,可以查看文件laravel ui中的authroutemethods:
return function ($options = []) { // 登錄路由... if ($options['login'] true) { $this->get('login', 'auth\\\\logincontroller@showloginform')->name('login'); $this->post('login', 'auth\\\\logincontroller@login'); } // 登出路由... if ($options['logout'] true) { $this->post('logout', 'auth\\\\logincontroller@logout')->name('logout'); } // 注冊(cè)路由... if ($options['register'] true) { $this->get('register', 'auth\\\\registercontroller@showregistrationform')->name('register'); $this->post('register', 'auth\\\\registercontroller@register'); } // 密碼重設(shè)路由... if ($options['reset'] true) { $this->resetpassword(); } // 密碼確認(rèn)路由... if ($options['confirm'] class_exists($this->prependgroupnamespace('auth\\\\confirmpasswordcontroller'))) { $this->confirmpassword(); } // 郵箱驗(yàn)證路由... if ($options['verify'] false) { $this->emailverification(); }};技巧 2. laravel ui: 僅生成控制器
官方文檔指定了使用 laravel ui 的主要方法:
php artisan ui vue --auth但是,如果您不需要可視 ui,該怎么辦?如果您創(chuàng)建的是一個(gè)僅基于 api 的項(xiàng)目,且在框架中沒(méi)有任何前端呢?
您仍然可以使用 laravel auth 及其控制器。安裝 laravel ui 并運(yùn)行以下命令:
php artisan ui:controllers 它只會(huì)生成 app/http/controllers/auth , 因此您不需要 blade 或 vue 文件即可使用它們。
請(qǐng)?jiān)?github 存儲(chǔ)庫(kù) 中參閱這個(gè) artisan 命令的實(shí)現(xiàn)。
技巧 3. 對(duì)敏感操作重新認(rèn)證密碼
您是否曾經(jīng)維護(hù)過(guò) github 存儲(chǔ)庫(kù),并試圖更改其訪(fǎng)問(wèn)設(shè)置?然后,github 要求您再次輸入密碼,以確保確實(shí)是您在操作。
從 laravel 6.2 開(kāi)始,框架中也集成了該功能。
您只需要向要保護(hù)的路由添加一個(gè)名為password.confirm的中間件即可。
route::get('/secrets', 'secretscontroller@show')->middleware('password.confirm');dries vints 引用自官方功能發(fā)布文章:
如果嘗試訪(fǎng)問(wèn)該路由,將提示您確認(rèn)密碼,和在 github 等其他應(yīng)用程序上看到的一樣。
確認(rèn)密碼后,默認(rèn)情況下會(huì)在用戶(hù)會(huì)話(huà)中存儲(chǔ)一個(gè)時(shí)間戳。時(shí)間戳持續(xù)3個(gè)小時(shí),因此用戶(hù)在此期間不必再次輸入密碼。
您可以使用auth配置文件中的password_timeout配置選項(xiàng)來(lái)自定義此持續(xù)時(shí)間。
技巧 4. 注銷(xiāo)其他設(shè)備
從 laravel 5.6 起,我們提供了一種單獨(dú)的方法來(lái)自動(dòng)注銷(xiāo)使用我們的帳戶(hù)登錄的任何其他設(shè)備或?yàn)g覽器:
auth::logoutotherdevices($password);典型的用法是在當(dāng)前設(shè)備成功登錄后注銷(xiāo)其他設(shè)備。為此,我們從 traitauthenticatesusers.php中重寫(xiě)方法authenticated(),并將其放入app / http / controllers / auth / logincontroller.php中:
protected function authenticated(request $request, $user){ \\\\auth::logoutotherdevices(request('password'));}另外,不要忘記激活app / http / kernel.php文件中的中間件authenticatesession,默認(rèn)情況下該中間件已被注釋?zhuān)?br>protected $middlewaregroups = [ 'web' => [ \\\\app\\\\http\\\\middleware\\\\encryptcookies::class, \\\\illuminate\\\\cookie\\\\middleware\\\\addqueuedcookiestoresponse::class, \\\\illuminate\\\\session\\\\middleware\\\\startsession::class, // \\\\illuminate\\\\session\\\\middleware\\\\authenticatesession::class, \\\\illuminate\\\\view\\\\middleware\\\\shareerrorsfromsession::class, \\\\app\\\\http\\\\middleware\\\\verifycsrftoken::class, \\\\illuminate\\\\routing\\\\middleware\\\\substitutebindings::class, ],登錄/注冊(cè)后的重定向:自定義邏輯
默認(rèn)情況下,laravel 的 logincontroller 和 registercontroller 具有相同的屬性:
class registercontroller extends controller{ protected $redirectto = routeserviceprovider::home;因此,您可以指定成功登錄/注冊(cè)后重定向到的 url。默認(rèn)值在 app/providers/routeserviceprovider.php中:
class routeserviceprovider extends serviceprovider{ public
上一個(gè):馬蘭種植新技術(shù)
下一個(gè):樓宇智能化過(guò)程中結(jié)構(gòu)化綜合布線(xiàn)系統(tǒng)的部署!

多聲道超聲波流量計(jì)的主要特點(diǎn)介紹
廚房油水分離器怎么使用,油水分離器應(yīng)該怎么使用?
家用臺(tái)式電腦怎么選配置好,家庭臺(tái)式電腦如何配置好
SMC增壓閥VBA40-04GN原理您知道多少?
定制生產(chǎn)實(shí)驗(yàn)室污水處理系統(tǒng)
sai2怎么把圖片導(dǎo)入圖層放大(sai2怎么把圖片導(dǎo)入圖層縮小)
地磅功能特點(diǎn)
什么是汽油的辛烷值?
調(diào)研B1級(jí)橡塑保溫管的市場(chǎng)擴(kuò)展
寧波機(jī)床防護(hù)罩保護(hù)機(jī)床也保護(hù)工作人員
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos