寻找一些帮助为我的laravel项目添加政策 [英] Looking for some helps in adding policy to my laravel projects

查看:71
本文介绍了寻找一些帮助为我的laravel项目添加政策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学习laravel的新手.目前,我一直受Laravel政策的困扰,请您指导我如何为我的项目添加政策?

I am new in learning laravel. Currently I have been stuck in the Laravel Policy,could you please kindly help with directing me how to add policy to my project?

我想仅使管理员用户能够使用Laravel策略规则来查看管理仪表盘",但失败了.实际上,每个注册用户都可以看到该入口(就像下面显示的所附图片一样).

I would like to make just only the Administrator User be able to see the 'Administration Dashboard' by using Laravel Policies Rules but failed. Actually every registered user is able to see that entrance(just like the attached picture showing below).

用户名为uu@gmail.com,密码为uuuuuu. 请访问我的测试网站 http://lookoko.com/并输入您将看到的用户名和密码下拉列表中的管理仪表板"列表.

The user name is uu@gmail.com and the password is uuuuuu. Please go to my testing website http://lookoko.com/ and log with the user name and password you will see that Administration Dashboard list in the drop-down lists.

推荐答案

要创建新的中间件,请使用make:middleware Artisan命令:

To create a new middleware, use the make:middleware Artisan command:

php artisan make:middleware AdminMiddleware

此命令将在app/Http/Middleware目录中放置一个新的AdminMiddleware类.在此中间件中,仅当登录用户为admin时,我们才允许访问路由.否则,我们会将用户重定向回home URI.

This command will place a new AdminMiddleware class within your app/Http/Middleware directory. In this middleware, we will only allow access to the route if the logged-in user is admin. Otherwise, we will redirect the users back to the home URI.

<?php

namespace App\Http\Middleware;

use Closure;

class AdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (! auth()->user()->is_admin) {
            return redirect()->route('home');
        }

        return $next($request);
    }
}

这里我假设您在用户表中有一个名为is_admin

注册中间件

您应该在app/Http/Kernel.php文件中为中间件分配一个密钥.默认情况下,此类的$routeMiddleware属性包含Laravel随附的中间件的条目.要添加您自己的,只需将其添加到此列表中,然后为其分配一个您选择的密钥即可.例如:

You should assign the middleware a key in your app/Http/Kernel.php file. By default, the $routeMiddleware property of this class contains entries for the middleware included with Laravel. To add your own, simply append it to this list and assign it a key of your choosing. For example:

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    ...
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'admin' => \App\Http\Middleware\AdminMiddleware::class,
];

一旦在HTTP内核中定义了中间件,则可以使用group方法将中间件分配给路由组:

Once the middleware has been defined in the HTTP kernel, you may use the group method to assign middleware to group of routes:

Route::group(['middleware' => ['admin']], function () {
    // all your admin routes comes here
});

文档

这篇关于寻找一些帮助为我的laravel项目添加政策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆