Laravel5.5添加新路由文件并制定规则
Laravel5.5里面有4个默认的路由文件,其中web.php是默认路由文件,如果需要添加其他路由文件,按照以下步骤进行。
此处以添加网站home前端路由举例,我已经先在/app/Http/Controller/文件夹下创建了一个Home文件夹,这个文件夹下主要放网站前端控制器,其他步骤如下:
1. 在项目routes目录下添加路由文件home.php;
2. 修改/app/providers/RouteServiceProvider.php
(1)添加路由方法
protected function mapHomeRoutes()
{Route::prefix('home')->middleware('home')->namespace($this->namespace.'\Home')->group(base_path('routes/home.php'));
}
(2)将添加的路由方法加入map方法中执行
public function map()
{$this->mapApiRoutes();$this->mapWebRoutes();$this->mapHomeRoutes(); // 添加执行的路由方法
}
附完整的RouteServiceProvider.php代码:
<?phpnamespace App\Providers;use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;class RouteServiceProvider extends ServiceProvider
{/*** This namespace is applied to your controller routes.** In addition, it is set as the URL generator's root namespace.** @var string*/protected $namespace = 'App\Http\Controllers';/*** Define your route model bindings, pattern filters, etc.** @return void*/public function boot(){//parent::boot();}/*** Define the routes for the application.** @return void*/public function map(){$this->mapApiRoutes();$this->mapWebRoutes();$this->mapHomeRoutes();}/*** Define the "web" routes for the application.** These routes all receive session state, CSRF protection, etc.** @return void*/protected function mapWebRoutes(){Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));}/*** Define the "api" routes for the application.** These routes are typically stateless.** @return void*/protected function mapApiRoutes(){Route::prefix('api')->middleware('api')->namespace($this->namespace)->group(base_path('routes/api.php'));}protected function mapHomeRoutes(){Route::prefix('home')->middleware('home')->namespace($this->namespace.'\Home')->group(base_path('routes/home.php'));}}
3. 在/app/Http/Kernel.php中添加home类名及其路径
protected $routeMiddleware = ['auth' => \Illuminate\Auth\Middleware\Authenticate::class,'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,'can' => \Illuminate\Auth\Middleware\Authorize::class,'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,// 根据之前设置的路由规则名(home)对应添加home类名,并指向路由验证路径'home' => \App\Http\Middleware\VerifyHome::class,
];
4. 在/app/Http/Middleware/文件夹下创建VerifyHome.php,并写入验证代码
代码如下:
<?php
namespace App\Http\Middleware;
use Closure;class VerifyHome
{public function handle($request, Closure $next){// if ("判断条件") {return $next($request);// }// 返回跳转到网站首页// return redirect('/');}
}
上面没有执行对home路由请求的验证,如果有需要自己加上。
5. 测试举例
(1)在home.php路由里添加两条路由规则,代码如下:
<?php
Route::get('aaa', 'IndexController@index');
Route::get('sss', 'IndexController@home');
(2)在/app/Http/Controller/Home/文件夹下创建IndexController.php,创建方式可以直接在文件夹下创建文件,也可以使用工具匠( php artisan make:controller Home\UserController ),控制器代码如下:
<?phpnamespace App\Http\Controllers\Home;use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;class IndexController extends Controller
{public function index(){echo "111222";}public function home(){echo "333";}
}
(3)访问测试:
a. 访问 laravel.com/home/aaa
b. 访问 laravel.com/home/bbb
注意:访问默认路由web.php下的规则不用加web,访问其他路由文件需要加上在RouteServiceProvider.php中定义的路由名。
ok,可以按照你的模块架构分路由文件了!
因为确实这是第一次用laravel5.5,所以文中如有不足,请留言告知一下哈,感谢!
Laravel5.5添加新路由文件并制定规则相关推荐
- 如何在win10新建菜单添加新的文件类型
介绍如何在win10新建菜单添加新的文件类型. 在新建菜单中添加新的文件类型 这里以.eps文件为例 原始新建菜单如下图: 第一步:打开注册表 快捷键win + R,输入regedit 第二步:在HK ...
- ubuntu添加路由_如何在Ubuntu,Linux中添加新路由?
ubuntu添加路由 I have a box with Ubuntu Linux and I want to add a new route to my box. Because I want to ...
- network-scripts目录下添加新网卡文件方法、nmcli修改NAME网卡名称和DEVICE一致
文章目录 说明 开启NetworkManager 添加网卡文件方式1[不建议用] 添加网卡文件方式2[建议用] 说明 在VMware的一台正在使用中的虚拟机中添加了几个新的网络适配器[已连接的那种] ...
- git cmd 命令在已有的仓库重新添加新的文件夹
正确步骤: 1. git init //初始化仓库 git add .(文件name) //添加文件到本地仓库 git commit -m "first commit" //添加 ...
- source insight 4.0添加新的文件类型
在阅读内核代码的时候,需要阅读设备树相关的代码,其后缀是 .dts .dtsi 像这种后缀,source insight 默认是不识别的.因此需要手动添加. 添加的方式: 在 option -& ...
- 【Vue】添加新页面
在[src\components\]路径下添加新vue文件,例如下图(图省事直接就复制了HelloWorld.vue,修改了一下里面的内容,需要懂html基础即可) 注意修改name 在[src\ro ...
- Linux下对文件的操作及添加新用户
Linux下对文件的操作及添加新用户 一.对文件的操作 1.打包压缩文件 2.解压缩文件 3.对文件操作的其他命令 二.创建新用户 一.对文件的操作 1.打包压缩文件 2.解压缩文件 3.对文件操作的 ...
- android 按键kl文件,Android添加新按键
1.前言 在Android开发中可能会遇到添加新的按键的需求,本文将简单介绍如何在Android系统中完成一个新的按键的添加. 当系统有新的按键需要添加时,Linux内核下的键码到Android系统中 ...
- sql server 快照_添加新文章,删除文章,更改快照文件夹路径和SQL Server复制中的数据筛选器行
sql server 快照 In the last articles, we have learned Configuring Snapshot and Transactional SQL Serve ...
最新文章
- 一直在纠结怎样取出 combox 控件elect的文字
- eve模拟器_《Re:从零开始的新伊甸征程》一步到位教你下载EVE手游
- cf375D. Tree and Queries(莫队)
- 企业级 SpringCloud 教程 (七) 高可用的分布式配置中心(Spring Cloud Config)
- 文件,多级目录的拷贝 笔记学习
- 根据数字生成唯一数字_PLSQL——根据上下数字生成等差数列补充空白行
- 我有一朋友生意做的红红火火
- win10 安装db2 10.1 并使用DBserver连接db2数据库
- css布局:多列等高布局
- 观察者模式Java内存管理进阶篇——如何避免内存泄漏
- 理解AutoResetEvent与ManualResetEvent的区别
- RainMeter — 使用 NotePad ++ 编辑皮肤文件
- ECMASript 6 新特性
- Cypress 前端测试工具的基本使用和相关命令总结
- 深信服AC1000路由部署模式怎么配置线路负载均衡
- 星际争霸php,星际争霸之php简单工厂模式
- Springboot+Mybatis接口快速上手
- 阿里1688产品图片和视频资料下载
- 对Java中常见的四种I/O模型理解
- php网页设计课程设计dreamweaver8_Dreamweaver8+PHP动态网站开发从入门到精通(附光盘)...