当我们把路由写到一个文件中时,路由显得杂乱不堪,不利于维护,这时我们需要将laravel路由进行分离

实现步骤:

   1、首先在app/Https/Controlles/下建立 Frontend(前端)、 Backend(后端)、 API(接口) 等文件夹;

    Laravel前后台+API路由分离架构(完善)

   2、在config文件夹下建立route.php配置文件

   Laravel前后台+API路由分离架构(完善)

   3、在route.php配置文件中配置要分离的主机地址(前、后台和API专属域名地址)。

    比如我的配置如下:

    Laravel前后台+API路由分离架构(完善)

    :如果在本机做测试,可以设置多个虚拟主机,虚拟主机设置方法参考:

    https://www.cnblogs.com/xiaoqian1993/p/6063375.html(百度随机找的,有很多)

    如果是wampserver集成环境,参考:wampserver配置虚拟主机

   4、在app/Https/建立对应的路由文件

   Laravel前后台+API路由分离架构(完善)

   5、打开app/Providers/RouteServiceProvider.php 定义各个功能对应的路由文件

   Laravel前后台+API路由分离架构(完善) 

    代码如下:

[php] view plain copy
  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. }  

至此,laravel路由分离配置就结束了。访问laravel_home.com就访问到前台的路由,laravel_admin.com就访问到后台的路由,laravel_api.com就访问API路由。

祝好运!


参考文章:laravel实现前后台路由分离

相关文章: