惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
PCI Perspectives
PCI Perspectives
T
Tailwind CSS Blog
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
V
V2EX
D
Docker
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
H
Help Net Security
The Register - Security
The Register - Security
宝玉的分享
宝玉的分享
C
Check Point Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
P
Privacy International News Feed
G
Google Developers Blog
博客园 - Franky
爱范儿
爱范儿
T
Tor Project blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
G
GRAHAM CLULEY
雷峰网
雷峰网
Cyberwarzone
Cyberwarzone
人人都是产品经理
人人都是产品经理
C
Cybersecurity and Infrastructure Security Agency CISA
Vercel News
Vercel News
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
F
Fortinet All Blogs
AWS News Blog
AWS News Blog
The Cloudflare Blog
C
CERT Recently Published Vulnerability Notes
I
InfoQ
Spread Privacy
Spread Privacy
T
Tenable Blog

mafeifan 的编程技术分享

mafengwo-mp3-downloader | mafeifan 的编程技术分享 示例页面 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 查看 default namespace 下的 default service account名称 mafeifan 的编程技术分享 检查日志 | mafeifan 的编程技术分享 bridge fdb show dev flannel.1 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 数据收集原理分析 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 几种方法 | mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 check the export on the very top of this document so we can use $do mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 mafeifan 的编程技术分享 延伸 -- 关于前端路由 xdebug 3 和 2 的区别
mafeifan 的编程技术分享
2026-01-16 · via mafeifan 的编程技术分享

先看 文档 Laravel 中间件提供了一种方便的机制来过滤进入应用的 HTTP 请求。 这里实现一个只有admin角色才能访问特定路由的功能

  1. 新建middleware php artisan make:middleware MustBeAdmin

  2. 打开生成的 \app\Http\Middleware\MustBeAdmin.php 修改handle方法 关于hasRole方法上一篇有讲解 这里在请求前判断用户角色是否是admin,如果条件满足进到下一个中间件。不满足返回首页。

    public function handle($request, Closure $next)
    {
    	   // 前置
	    if ($request->user()->hasRole('admin')) {
		    return $next($request);
	    }
	    return redirect('/');
    }
  1. 让系统识别中间件。打开 \app\Http\Kernel 在 $routeMiddleware 数组里追加 'mustAdmin' => \App\Http\Middleware\MustBeAdmin::class,

  2. 关于中间件的调用非常灵活,比如

  • 在 routes\web.php 中 Route::resource('posts', 'PostsController')->middleware('mustAdmin');
  • 在控制器中
class PostsController extends Controller
{

	public function __construct()
	{
		$this->middleware('mustAdmin', ['only' => 'show']);
	}
...
  1. 项目中用到过的中间件

例1 ​

在route中定义哪些角色可以访问,通过 role:ADMIN,TEACHER 知,role是中间件名字,后面的 ADMIN,TEACHER 是参数。 routes.php

Route::group(['middleware' => ['web', 'auth', 'role:ADMIN,TEACHER'], 'namespace' => '\StudentTrac\Guides\Controllers'],
    function () {
        Route::resource('guides', 'GuidesController', ['only' => ['index']]);
        Route::resource('guides/admin', 'AdminController', ['only' => ['index', 'edit']]);
    }
);

/app/Http/Middleware/Role.php

    public function handle($request, Closure $next, $role)
    {
        //  ['ADMIN', 'TEACHER']
    	$roles = func_get_args();
    	$roleIds = [];
        // 根据role名字拿到对应的id
		foreach ($roles as $index => $role) {
            // 为什么这么判断我也忘了
			if ($index < 2) continue;
			$roleIds[] = config('roles.' . trim($role));
		}

        // 判断当前用户的roleId是否存在
        if (! in_array((int)$this->auth->user()->RoleId, $roleIds)) {
            return response('Unauthorized', 403);
        }

        return $next($request);
    }

config/roles.php

return [
    /*
     * Role id for role.
     */
    'ADMIN'    => 1,
    'STUDENT'  => 2,
    'GUARDIAN' => 3,
    'TEACHER'  => 4,
    'SUPPORTSTAFF' => 5,
    'AUDITOR' => 6,
    'CURRICULUM' => 7,
    'CLIENTADMINISTRATOR' => 8,
];