Home >PHP Framework >Laravel >How to build multiple sites locally in Laravel

How to build multiple sites locally in Laravel

藏色散人
藏色散人forward
2020-11-30 14:35:004794browse

The following tutorial column of Laravel Framework will introduce you to how to build multiple sites locally with Laravel. I hope it will be helpful to friends who need it!

How to build multiple sites locally in Laravel

##Preface

I have a lot of thoughts recently. I want to do this and that. But I encountered a very uncomfortable problem:

    I haven’t organized all the previous codes, and there is no storage path or plan.
  1. There is no unified management solution for these codes.
So for me, the most important thing right now is to organize the code first, and then develop a set of localized management tools of my own.

This tool is currently tentatively planned to be developed for Laravel. It adopts the strategy of separating the front and back ends so that multiple terminals can have corresponding APIs for use in the future.

And I don’t particularly like to open multiple terminals. Several Laravels are being developed, so including multiple domain names in a Laravel framework has become one of the main problems.

Today I will record it carefully. I use Laravel to set up multiple domain names in localization. .In fact, the establishment of other systems is almost the same.

Plan

Currently decided to divide it into two domain names.

One is the interface domain name of the API, which I decided to be :

api.hellolux.com

One is the background management domain name, I set it as:

admin.hellolux.com

implementation

Add a new folder in the Controller layer

In the app\Http\Controllers directory, add two new folders, namely Api and Admin.

Modify the RouteServiceProvider.php file

In app\Providers\RouteServiceProvider.php, modify

# 新增项目名称的命名空间
protected $AdminNamespace = 'App\Http\Controllers\Admin';
protected $ApiNamespace = 'App\Http\Controllers\Api';

public function map()
{
    # 根据项目名称定义路由
    $this->mapApiRoutes();
    $this->mapAdminRoutes();
}

# 新增两个方法
protected function mapAdminRoutes()
{
    Route::group([
        'domain' => config('app.admin_domain'),
        'namespace' => $this->AdminNamespace,
    ], function ($router) {
        require base_path('routes/admin.php');
    });
}
protected function mapApiRoutes()
{
    Route::group([
        'domain' => config('app.api_domain'),
        'namespace' => $this->ApiNamespace,
    ], function ($router) {
        require base_path('routes/api.php');
    });
}

Add

'api_domain' => env('API_DOMAIN', 'api.hellolux.com'),
'admin_domain' => env('ADMIN_DOMAIN', 'admin.hellolux.com'),

in config/app.php.Add

API_DOMAIN=api.hellolux.com
ADMIN_DOMAIN=admin.hellolux.com

in .env In the routes directory, add two new files, api.php and admin.php

# api.php
use Illuminate\Http\Request;

Route::get('/', "IndexController@index");



# admin.php
use Illuminate\Http\Request;

Route::get('/', "IndexController@index");

Add the domain name to /etc/hosts

# Local_Manage
127.0.0.1   api.hellolux.com
127.0.0.1   admin.hellolux.com

Cancel vhosts in /etc/apache2/http.conf Comment

Include /private/etc/apache2/extra/httpd-vhosts.conf

In /etc/apache2/extra/httpd-vhosts.conf, add

<VirtualHost *:80>
    ServerAdmin hellolux@163.com
    DocumentRoot "/Users/hellolux/Documents/Github/Local_Manage/public"
    ServerName hellolux
    ServerAlias *.hellolux.com
    ErrorLog "/Users/hellolux/Documents/Github/Local_Manage/logs/error.log"
    CustomLog "/Users/hellolux/Documents/Github/Local_Manage/logs/access.log" common
</VirtualHost>

Restart apache

sudo apachevtl restart

Complete

Browser Opening admin.hellolux.com and api.hellolux.com will display specific pages respectively.

The above is the detailed content of How to build multiple sites locally in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete