laravel前后台路由分离
在laravel中创建文件放置前台和后台控制器
找到app/providers/RouteServiceProvider.PHP文件
在内配置
例:
<?php namespace App\Providers; use Illuminate\Routing\Router; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; class RouteServiceProvider extends ServiceProvider { /** * This namespace is applied to the controller routes in your routes file. * * In addition, it is set as the URL generator's root namespace. * * @var string */ protected $namespace = 'App\Http\Controllers'; protected $frontendNamespace; /** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { // $this->frontnamespace = 'App\Http\Controllers\Front'; parent::boot($router); } /** * Define the routes for the application. * * @param \Illuminate\Routing\Router $router * @return void */ public function map(Router $router) { //配置路由所在文件 // $backendUrl = config('route.backend_url'); // $frontendUrl = config('route.frontend_url'); // $apiUrl = config('route.api_url'); // $router->group(['namespace' => $this->namespace], function ($router) { require app_path('Http/routes.php'); }); //前台 $router->group(['namespace' => $this->frontnamespace], function ($router) { // 'domain' => $backendUrl, require app_path('Http/routes_front.php'); }); } }
或
<?php namespace App\Providers; use Illuminate\Routing\Router; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; class RouteServiceProvider extends ServiceProvider { /** * This namespace is applied to the controller routes in your routes file. * * In addition, it is set as the URL generator's root namespace. * * @var string */ protected $namespace = 'App\Http\Controllers'; protected $backendNamespace; protected $frontendNamespace; protected $apiNamespace; protected $currentDomain; /** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { // $this->backendNamespace = 'App\Http\Controllers\Backend'; $this->frontendNamespace = 'App\Http\Controllers\Frontend'; $this->apiNamespace = 'App\Http\Controllers\API'; // $this->currentDomain = $this->app->request->server->get('HTTP_HOST'); $this->currentDomain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ""; parent::boot($router); } /** * Define the routes for the application. * * @param \Illuminate\Routing\Router $router * @return void */ public function map(Router $router) { // $router->group(['namespace' => $this->namespace], function ($router) { // require app_path('Http/routes.php'); // }); $backendUrl = config('route.backend_url'); $frontendUrl = config('route.frontend_url'); $apiUrl = config('route.api_url'); switch ($this->currentDomain) { case $apiUrl: // API路由 $router->group([ 'domain' => $apiUrl, 'namespace' => $this->apiNamespace], function ($router) { require app_path('Http/routes-api.php'); } ); break; case $backendUrl: // 后端路由 $router->group([ 'domain' => $backendUrl, 'namespace' => $this->backendNamespace], function ($router) { require app_path('Http/routes-backend.php'); } ); break; default: // 前端路由 $router->group([ 'domain' => $frontendUrl, 'namespace' => $this->frontendNamespace], function ($router) { require app_path('Http/routes-frontend.php'); } ); break; } } }
完成后我们的路由也可以新建了 但要和上面的名称要一样
在路由中可以这样写(当然也可以自定义路由)例:
个人主页
Route::group(['middleware' => ['web']], function () { Route::controller('/test', 'TestController'); // 重置 Route::get('user/password/reset/{token?}', [ 'as' => 'user.password.reset@token', 'uses' => 'User\PasswordController@getReset' ]); ]);
作者:子钦加油
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年