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

实现步骤:

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

    


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

   


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

    比如我的配置如下:


    


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

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

    如果是wampserver集成环境,参考:wampserver配置虚拟主机
<https://blog.csdn.net/createno_1/article/details/79818491>

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

   


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


    

    代码如下:
[php] view plain
<https://blog.csdn.net/woshihaiyong168/article/details/53468177#> copy
<https://blog.csdn.net/woshihaiyong168/article/details/53468177#>
* <?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;  
*         }  
*   
*     }  
* }  

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


祝好运!




参考文章:laravel实现前后台路由分离
<https://blog.csdn.net/woshihaiyong168/article/details/53468177>


友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信