本篇文章給大家帶來了關于laravel的相關知識,其中主要介紹了laravel nova是什么?laravel中應用中怎么模擬用戶?感興趣的朋友,下面一起來看一下,希望對大家有幫助。
Laravel Nova 的一個新特性是在控制面板中模擬用戶。這很方便,原因很多。但對于我而言,當收到錯誤報告或問題,并希望看到用戶所看到的內容時,模擬他們可以節省大量時間,因為您可以看到他們所看到的。
如果你也想在你的 Laravel 應用中實現該功能,Laravel Impersonate 包讓這一點變得簡單。
步驟 1. 安裝軟件包
composer require lab404/laravel-impersonate
然后,打開 config/app.php 并將其添加都 providers 數組:
'providers'?=>?[ ????//?... ????Lab404ImpersonateImpersonateServiceProvider::class, ],
之后,打開 Models/User 并添加 trait:
use?Lab404ImpersonateModelsImpersonate; class?User?extends?Authenticatable { ????use?Impersonate;
步驟 2. 模擬路由
Laravel Impersonate 包包含了一些模擬用戶的方法,不過我發現將路由宏添加到 routes/web.php 文件中是最為簡便的方法:
Route::impersonate();
這給你一些命名路由:
//?Where?$id?is?the?ID?of?the?user?you?want?to?impersonate route('impersonate',?$id) //?Or?in?case?of?multi?guards,?you?should?also?add?`guardName`?(defaults?to?`web`) route('impersonate',?['id'?=>?$id,?'guardName'?=>?'admin']) //?Generate?an?URL?to?leave?the?current?impersonation route('impersonate.leave')
步驟 3. Laravel Blade 模擬用例
Laravel Impersonate 設置就緒后,你可以使用 其中模板 helpers:
@canImpersonate($guard?=?null) ????<a>id)?}}">Impersonate?this?user</a> @endCanImpersonate
然后反轉:
@impersonating($guard?=?null) ????<a>Leave?impersonation</a> @endImpersonating
步驟 4. 高級設置
另一個你可能會考慮的是,限制誰可以模擬其他用戶,以及那些用戶可以被模擬。在 Models/User 中,你可以添加以下方法:
/** ?*?By?default,?all?users?can?impersonate?anyone ?*?this?example?limits?it?so?only?admins?can ?*?impersonate?other?users ?*/ public?function?canImpersonate():?bool { ????return?$this->is_admin(); } /** ?*?By?default,?all?users?can?be?impersonated, ?*?this?limits?it?to?only?certain?users. ?*/ public?function?canBeImpersonated():?bool { ????return?!?$this->is_admin(); }
推薦學習:《laravel視頻教程》? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END