单个 Laravel 项目同时配置不同域名

第一步:添加app\http\Controllers文件夹里面创建我们要存放前端和后端或者接口的文件夹

列如: Home(前端)  Admin(后端)   App(接口)  文件夹
  •  

这里写图片描述

第二步:修改app\http\providers\RouteServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';
    protected $homeNamespace = 'App\Http\Controllers\Home';//PC端
    protected $adminNamespace = 'App\Http\Controllers\Admin';//管理后台

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        //$this->mapApiRoutes();

        //$this->mapWebRoutes();
        $sld_prefix = explode('.',$_SERVER['HTTP_HOST'])[0];
        if(config('route.admin_url') == $sld_prefix){
            $this->mapAdminRoutes();
        }elseif(config('route.home_url') == $sld_prefix){
            $this->mapHomeRoutes();
        }elseif(config('route.api_url') == $sld_prefix){
            $this->mapApiRoutes();
        }
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

    /**
     * 管理后台
     */
    protected function mapAdminRoutes()
    {
        Route::middleware('web')
            ->namespace($this->adminNamespace)
            ->group(base_path('routes/admin.php'));
    }

    /**
     * PC端
     */
    protected function mapHomeRoutes()
    {
        Route::middleware('web')
            ->namespace($this->homeNamespace)
            ->group(base_path('routes/home.php'));
    }
}

第三步:在config下创建文件route.php,并在里面添加:

<?php
return array(
        'admin_url'=>'admin',
        'home_url'=>'www',
        'api_url'=>'api'
);

第四步:在routes目录下创建admin.php 和home.php 路由 
这里写图片描述

第五步:分别在app\Http\Controllers\Admin和app\Http\Controllers\Home

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{
    public function index()
    {
        echo "this is admin";
    }
}


<?php
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        echo "this is home";
    }
}

第六步:分别在admin.php 和home.php 新建路由

Route::get('/', 'AdminController@index');
  •  
Route::get('/','HomeController@index');

第七步:添加nginx配置日志

server {
        listen       80;
        listen       443 ssl;
        server_name  www.laravel.com admin.laravel.com;

        charset utf-8;

        #access_log  logs/www.polypm.com.cn.access.log  main;

        root   /data/webroot/kangsf.com.cn/public;
        index  index.html index.php;

              ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
              ssl_certificate ssl/cert.pem;
              ssl_certificate_key ssl/cert.key;


        location / {
            try_files $uri $uri/ /index.php?$args;
        }


        # redirect server error pages to the static page /50x.html
        #
        #error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
        #    root   html;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:5443
        #

       location ~ \.php$ {
            fastcgi_pass   127.0.0.1:7113;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

第八步:测试 
这里写图片描述

这里写图片描述

原文转载参考:https://blog.youkuaiyun.com/u013257111/article/details/78768603,在测试过程中遇到一些问题。所以有所添加,希望@LaooGao 博友不支介意。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值