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'));
}
如果不想添加路由前缀可以去掉
protected function mapHomeRoutes()
{
Route::namespace($this->namespace.'\Home')
->group(base_path('routes/home.php'));
}
(2)将添加的路由方法加入map方法中执行
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapHomeRoutes(); // 添加执行的路由方法
}
附完整的RouteServiceProvider.php代码:
<?php
namespace 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. 创建文件VerifyHome.php
代码如下:
<?php
namespace App\Http\Middleware;
use Closure;
class VerifyHome
{
public function handle($request, Closure $next)
{
// if ("判断条件") {
return $next($request);
// }
// 返回跳转到网站首页
// return redirect('/');
}
}
在/app/Http/Kernel.php中添加home类名及其路径
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
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' => \App\Http\Middleware\VerifyHome::class,
'wechat' => \App\Http\Middleware\VerifyWechat::class,
];
上面没有执行对home路由请求的验证,如果有需要自己加上。
5. 测试举例
(1)在home.php路由里添加两条路由规则,代码如下:
<?php
Route::get('login', 'LoginController@login');
(2)在/app/Http/Controller/Home/文件夹下创建LoginController.php,创建方式可以直接在文件夹下创建文件,也可以使用工具匠( php artisan make:controller Home\UserController ),控制器代码如下:
<?php
namespace App\Http\Controllers\home;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Routing\Controller as BaseController;
class LoginController extends BaseController
{
public function login(Request $request){
echo 11111;exit;
}
}
在Kernel.php 文件中 代码如下