php的后台接口分离框架,Laravel前后台+API路由分离架构(完善)

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

发布时间:2018-04-04 15:47,

浏览次数:428

, 标签:

Laravel

API

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

实现步骤:

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

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

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

比如我的配置如下:

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

https://www.cnblogs.com/xiaoqian1993/p/6063375.html

(百度随机找的,有很多)

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

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

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

代码如下:

[php] view plain

 copy

* <?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实现前后台路由分离

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值