Laravel應(yīng)用中模擬用戶實(shí)例分析

今天小編給大家分享一下Laravel應(yīng)用中模擬用戶實(shí)例分析的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

從事綿陽(yáng)服務(wù)器托管,服務(wù)器租用,云主機(jī),雅安服務(wù)器托管,域名與空間,CDN,網(wǎng)絡(luò)代維等服務(wù)。

Laravel Nova 的一個(gè)新特性是在控制面板中模擬用戶。這很方便,原因很多。但對(duì)于我而言,當(dāng)收到錯(cuò)誤報(bào)告或問題,并希望看到用戶所看到的內(nèi)容時(shí),模擬他們可以節(jié)省大量時(shí)間,因?yàn)槟梢钥吹剿麄兯吹降摹?/p>

如果你也想在你的 Laravel 應(yīng)用中實(shí)現(xiàn)該功能,Laravel Impersonate 包讓這一點(diǎn)變得簡(jiǎn)單。

步驟 1. 安裝軟件包

composer require lab404/laravel-impersonate

然后,打開 config/app.php 并將其添加都 providers 數(shù)組:

'providers' => [
    // ...
    Lab404\Impersonate\ImpersonateServiceProvider::class,
],

之后,打開 Models/User 并添加 trait:

use Lab404\Impersonate\Models\Impersonate;

class User extends Authenticatable
{
    use Impersonate;

步驟 2. 模擬路由

Laravel Impersonate 包包含了一些模擬用戶的方法,不過我發(fā)現(xiàn)將路由宏添加到 routes/web.php 文件中是最為簡(jiǎn)便的方法:

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 設(shè)置就緒后,你可以使用 其中模板 helpers:

@canImpersonate($guard = null)
    <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
@endCanImpersonate

然后反轉(zhuǎn):

@impersonating($guard = null)
    <a href="{{ route('impersonate.leave') }}">Leave impersonation</a>
@endImpersonating

步驟 4. 高級(jí)設(shè)置

另一個(gè)你可能會(huì)考慮的是,限制誰(shuí)可以模擬其他用戶,以及那些用戶可以被模擬。在 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應(yīng)用中模擬用戶實(shí)例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:Laravel應(yīng)用中模擬用戶實(shí)例分析
瀏覽路徑:http://bm7419.com/article22/igcjjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)頁(yè)設(shè)計(jì)公司營(yíng)銷型網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、微信小程序全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)