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

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

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

单个 Laravel 项目同时配置不同域名_第1张图片

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

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,并在里面添加:

'admin',
        'home_url'=>'www',
        'api_url'=>'api'
);

第四步:在routes目录下创建admin.php 和home.php 路由 
单个 Laravel 项目同时配置不同域名_第2张图片

第五步:分别在app\Http\Controllers\Admin和app\Http\Controllers\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;
        #}
    }

第八步:测试 
单个 Laravel 项目同时配置不同域名_第3张图片

单个 Laravel 项目同时配置不同域名_第4张图片

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

你可能感兴趣的:(学习笔记)