laravel前后台路由分离

在laravel中创建文件放置前台和后台控制器

找到app/providers/RouteServiceProvider.PHP文件

在内配置

例:

[plain]  view plain  copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2.   
  3. namespace App\Providers;  
  4.   
  5. use Illuminate\Routing\Router;  
  6. use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;  
  7.   
  8. class RouteServiceProvider extends ServiceProvider  
  9. {  
  10.     /**  
  11.      * This namespace is applied to the controller routes in your routes file.  
  12.      *  
  13.      * In addition, it is set as the URL generator's root namespace.  
  14.      *  
  15.      * @var string  
  16.      */  
  17.     protected $namespace = 'App\Http\Controllers';  
  18.     protected $frontendNamespace;  
  19.     /**  
  20.      * Define your route model bindings, pattern filters, etc.  
  21.      *  
  22.      * @param  \Illuminate\Routing\Router  $router  
  23.      * @return void  
  24.      */  
  25.     public function boot(Router $router)  
  26.     {  
  27.         //  
  28.         $this->frontnamespace = 'App\Http\Controllers\Front';  
  29.         parent::boot($router);  
  30.     }  
  31.     /**  
  32.      * Define the routes for the application.  
  33.      *  
  34.      * @param  \Illuminate\Routing\Router  $router  
  35.      * @return void  
  36.      */  
  37.     public function map(Router $router)  
  38.     {  
  39.         //配置路由所在文件  
  40.         // $backendUrl = config('route.backend_url');  
  41.         // $frontendUrl = config('route.frontend_url');  
  42.         // $apiUrl = config('route.api_url');  
  43.                 //  
  44.                 $router->group(['namespace' => $this->namespace], function ($router) {  
  45.                     require app_path('Http/routes.php');  
  46.                 });  
  47.                 //前台  
  48.                 $router->group(['namespace' => $this->frontnamespace], function ($router)   
  49.                 {  
  50.                     // 'domain' => $backendUrl,  
  51.                     require app_path('Http/routes_front.php');  
  52.                 });  
  53.     }  
  54. }  




[plain]  view plain  copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2.   
  3. namespace App\Providers;  
  4.   
  5. use Illuminate\Routing\Router;  
  6. use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;  
  7.   
  8. class RouteServiceProvider extends ServiceProvider  
  9. {  
  10.     /**  
  11.      * This namespace is applied to the controller routes in your routes file.  
  12.      *  
  13.      * In addition, it is set as the URL generator's root namespace.  
  14.      *  
  15.      * @var string  
  16.      */  
  17.     protected $namespace = 'App\Http\Controllers';  
  18.     protected $backendNamespace;  
  19.     protected $frontendNamespace;  
  20.     protected $apiNamespace;  
  21.     protected $currentDomain;  
  22.   
  23.     /**  
  24.      * Define your route model bindings, pattern filters, etc.  
  25.      *  
  26.      * @param  \Illuminate\Routing\Router $router  
  27.      * @return void  
  28.      */  
  29.     public function boot(Router $router)  
  30.     {  
  31.         //  
  32.         $this->backendNamespace = 'App\Http\Controllers\Backend';  
  33.         $this->frontendNamespace = 'App\Http\Controllers\Frontend';  
  34.         $this->apiNamespace = 'App\Http\Controllers\API';  
  35. //        $this->currentDomain = $this->app->request->server->get('HTTP_HOST');  
  36.         $this->currentDomain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "";  
  37.   
  38.         parent::boot($router);  
  39.     }  
  40.   
  41.     /**  
  42.      * Define the routes for the application.  
  43.      *  
  44.      * @param  \Illuminate\Routing\Router $router  
  45.      * @return void  
  46.      */  
  47.     public function map(Router $router)  
  48.     {  
  49. //        $router->group(['namespace' => $this->namespace], function ($router) {  
  50. //            require app_path('Http/routes.php');  
  51. //        });  
  52.   
  53.         $backendUrl = config('route.backend_url');  
  54.         $frontendUrl = config('route.frontend_url');  
  55.         $apiUrl = config('route.api_url');  
  56.   
  57.         switch ($this->currentDomain) {  
  58.             case $apiUrl:  
  59.                 // API路由  
  60.                 $router->group([  
  61.                     'domain' => $apiUrl,  
  62.                     'namespace' => $this->apiNamespace],  
  63.                     function ($router) {  
  64.                         require app_path('Http/routes-api.php');  
  65.                     }  
  66.                 );  
  67.   
  68.                 break;  
  69.             case $backendUrl:  
  70.                 // 后端路由  
  71.                 $router->group([  
  72.                     'domain' => $backendUrl,  
  73.                     'namespace' => $this->backendNamespace],  
  74.                     function ($router) {  
  75.                         require app_path('Http/routes-backend.php');  
  76.                     }  
  77.                 );  
  78.                 break;  
  79.             default:  
  80.                 // 前端路由  
  81.                 $router->group([  
  82.                     'domain' => $frontendUrl,  
  83.                     'namespace' => $this->frontendNamespace],  
  84.                     function ($router) {  
  85.                         require app_path('Http/routes-frontend.php');  
  86.                     }  
  87.                 );  
  88.   
  89.                 break;  
  90.         }  
  91.   
  92.     }  
  93. }  

完成后我们的路由也可以新建了  但要和上面的名称要一样

在路由中可以这样写(当然也可以自定义路由)例:

个人主页

[plain]  view plain  copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. Route::group(['middleware' => ['web']], function () {  
  2.   
  3.     Route::controller('/test', 'TestController');  
  4.     // 重置  
  5.     Route::get('user/password/reset/{token?}', [  
  6.         'as' => 'user.password.reset@token',  
  7.         'uses' => 'User\PasswordController@getReset'  
  8.     ]);  
  9. ]);  

注:转自woshihaiyong168的博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值