当前位置:网站首页>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
边栏推荐
- Logback日志框架第三方jar包 免费获取
- Please supervise the 2022 plan
- 招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- Limit of total fields [1000] in index has been exceeded
- 分类模型评价标准(performance measure)
- How to implement backspace in shell
- After UE4 is packaged, mesh has no material problem
- TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
- Three. JS introductory learning notes 04: external model import - no material obj model
猜你喜欢

You Yuxi, coming!

讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

LeetCode3_ Longest substring without duplicate characters

UE4 exports the picture + text combination diagram through ucanvasrendertarget2d

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

保证接口数据安全的10种方案

SPI master rx time out中断

The unity vector rotates at a point
随机推荐
47_Opencv中的轮廓查找 cv::findContours()
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
神经网络c语言中的指针是怎么回事
20th anniversary of agile: a failed uprising
torch.numel作用
Mysql database basic operation DQL basic query
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
Plate - forme de surveillance par étapes zabbix
LeetCode2_ Add two numbers
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
numpy---基础学习笔记
Migration and reprint
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Limit of total fields [1000] in index has been exceeded
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
How does geojson data merge the boundaries of regions?
markdown公式编辑教程
TCP framework___ Unity
js中复选框checkbox如何判定为被选中