laravel5.2前后台分离

1.控制器
设置各自的命名空间

2.路由

三个文件里面都一样
        
3.修改RouteServiceProvider.php
将以下的代码复制进去替换掉
<?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 = 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');
        //var_dump($apiUrl);

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

    }
}

4.创建配置文件
上面为配置的域名,在自己本机配置域名(host文件),

配置完成,输入域名登陆相应控制器里的默认方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值