這篇文章給大家分享的是有關Laravel 5.4前后臺分離如何通過不同的二級域名訪問的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
第一步:添加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'));
}
}第三步:在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');
第六步:測試


第七步:運行報錯
錯誤一:laravel Class ‘App\Http\Controllers\Controller' not found
錯誤二:Class App\Http\Controllers\IndexController does not exist
解決方法:
在PHPstorm Terminal控制臺輸入“composer dump-autoload”
因為laravel是用composer來加載類,不是命令創建的類要更新autoload。
如果沒有使用PHPstorm編輯器的話,我們需要在本地安裝composer,然后cmd以管理員運行,進入到項目的根目錄執行“composer dump-autoload”
感謝各位的閱讀!關于“Laravel 5.4前后臺分離如何通過不同的二級域名訪問”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。