当前位置:首页 > CMS教程 > 其它CMS > 列表

laravel实现前后台路由分离的方法

发布:smiling 来源: PHP粉丝网  添加日期:2022-01-02 12:26:57 浏览: 评论:0 

今天小编就为大家分享一篇laravel实现前后台路由分离的方法,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

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

实现步骤:

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

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

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

代码如下:

  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. } 

此时只需要在不同的控制器中建立路由就 Ok了。

Tags: laravel前后台路由分离

分享到: