当前位置:网站首页>Laravel5.1 路由 -路由分组
Laravel5.1 路由 -路由分组
2022-07-07 14:06:00 【全栈程序员站长】
路由分组有啥好处?
有时候啊 一大堆路由它们都有共同的地方,比如都使用一个中间件(过两天写)或是前缀都一样,避免代码重复 我们可以将他们分到一组中。
1 路由分组可以共享哪些属性?
- 中间件 middleware。
- 控制器的命名空间 namespace。
- 子域名 domain
- 路由前缀
1.1 中间件
关于中间件大K还没有写笔记介绍,这里先简单说下 中间件就是接收到请求后验证一些东西或相应后验证一些东西,比如Laravel自带的Auth中间件 就是验证用户有没有登录进来,如果用户没用登录,那么就会自动跳转到登录页面,我们完全不用实现这一方面的逻辑。
好啦 回归正题,咱一块儿看看路由分组咋写:
/** * 这就是一个路由分组 /user和/user/profile都将使用auth中间件。 */ Route::group(['middleware' => 'auth'], function (){ Route::get('/user', function (){ }); Route::get('/user/profile', function (){ }); });
1.2 路由前缀
/** * 路由前缀呢 就是讲此分组中的所有路由路径前加个前缀 */ Route::group(['prefix' => 'admin'], function (){ /** * 路由分组是可以嵌套的哦 */ Route::group(['middleware' => 'auth'], function (){ /** * 这条路由不仅使用auth中间件,而且还加了admin前缀,我们通过/admin/user才能访问 */ Route::get('/user', function (){ }); Route::get('/user/profile', function (){ }); }); /** * 访问路径是:/admin */ Route::get('/', function (){ });; });
1.3 子域名
/** * 比如我们可以输入larger来访问路由,在子路由中可以通过参数来把larger取到。 */ Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', function ($account, $id) { // }); });
注意:如果想测试子域名你需要使用homestand来设置你的域名。注意:如果想测试子域名你需要使用homestand来设置你的域名。
1.4 命名空间
这又是一个没写的点 这里包含控制器的内容,先看例子吧 明天就学控制器了 倒时候做笔记。
/** * 只要指明了命名空间,那么在子路由中所使用的所有控制器都位于App\Http\Controller\Admin这个命名空间下。 */ Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function(){ /** * 其实HomeController在App\Http\Controller\Admin这个命名空间下。 */ Route::get('/', '[email protected]'); });
1.5 分组命名
补充一点,前一篇的基础文章我们学了给路由命名,那么分组可不可以命名呢,Yo Man。。当然可以:
/** * 跟普通路由一样 也是用as来命名但是首字母最好大写后面跟俩冒号 代表它是一个分组 如果这样写 我们就可以通过 route('Admin::index')方式来找到它了 */ Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'Admin::'], function(){ Route::get('/', ['as' => 'index','uses' => '[email protected]']); });
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113173.html原文链接:https://javaforall.cn
边栏推荐
- Three. JS introductory learning notes 10:three JS grid
- Shader basic UV operations, translation, rotation, scaling
- Mysql database basic operation DQL basic query
- hellogolang
- Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
- How does geojson data merge the boundaries of regions?
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- 121. 买卖股票的最佳时机
- AE learning 01: AE complete project summary
猜你喜欢
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
Logback logging framework third-party jar package is available for free
torch. Numel action
Postman generate timestamp, future timestamp
分步式监控平台zabbix
LeetCode2_ Add two numbers
Three. JS introductory learning notes 11:three JS group composite object
SPI master rx time out中断
Numpy -- epidemic data analysis case
Vite path alias @ configuration
随机推荐
A link opens the applet code. After compilation, it is easy to understand
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
LeetCode1_ Sum of two numbers
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address
Odoo集成Plausible埋码监控平台
Mesh merging under ue4/ue5 runtime
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
统计学习方法——感知机
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
分步式監控平臺zabbix
MySQL中, 如何查询某一天, 某一月, 某一年的数据
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
尤雨溪,来了!
three.js打造酷炫下雪效果
nodejs package. JSON version number ^ and~
Leetcode-136-只出现一次的数(用异或来解答)
C4D learning notes 1- animation - animation key frames
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages