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

吾爱主题 阅读:133 2021-09-01 15:34:00 评论:0

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

实现步骤:

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

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

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

代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 <?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 ;   }     } }

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

以上这篇laravel实现前后台路由分离的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/woshihaiyong168/article/details/53468177

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容