Home> PHP Framework> Laravel> body text

How to build multiple sites locally in Laravel

藏色散人
Release: 2020-11-30 14:35:22
forward
4575 people have browsed it

The following tutorial column ofLaravel Frameworkwill 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'); }); }
Copy after login

Add

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

in config/app.php.Add

API_DOMAIN=api.hellolux.com ADMIN_DOMAIN=admin.hellolux.com
Copy after login

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");
Copy after login

Add the domain name to /etc/hosts

# Local_Manage 127.0.0.1 api.hellolux.com 127.0.0.1 admin.hellolux.com
Copy after login

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

Include /private/etc/apache2/extra/httpd-vhosts.conf
Copy after login

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

 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 
Copy after login

Restart apache

sudo apachevtl restart
Copy after login
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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!