laravel實(shí)現(xiàn)前后臺路由分離的方法
當(dāng)我們把路由寫到一個(gè)文件中時(shí),路由顯得雜亂不堪,不利于維護(hù),這時(shí)我們需要將laravel路由進(jìn)行分離
實(shí)現(xiàn)步驟:
1、首先在app/Https/Controlles/文件下建立 Frontend(前端) Backend(后端) API(接口) 文件
2、在app/Https/建立對應(yīng)的路由文件

3、打開app/Providers/RouteServiceProvider.php 定義各個(gè)功能對應(yīng)的路由文件

代碼如下:
<?php
namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to the controller routes in your routes file.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
protected $backendNamespace;
protected $frontendNamespace;
protected $apiNamespace;
protected $currentDomain;
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function boot(Router $router)
{
//
$this->backendNamespace = 'App\Http\Controllers\Backend';
$this->frontendNamespace = 'App\Http\Controllers\Frontend';
$this->apiNamespace = 'App\Http\Controllers\API';
// $this->currentDomain = $this->app->request->server->get('HTTP_HOST');
$this->currentDomain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : "";
parent::boot($router);
}
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router)
{
// $router->group(['namespace' => $this->namespace], function ($router) {
// require app_path('Http/routes.php');
// });
$backendUrl = config('route.backend_url');
$frontendUrl = config('route.frontend_url');
$apiUrl = config('route.api_url');
switch ($this->currentDomain) {
case $apiUrl:
// API路由
$router->group([
'domain' => $apiUrl,
'namespace' => $this->apiNamespace],
function ($router) {
require app_path('Http/routes-api.php');
}
);
break;
case $backendUrl:
// 后端路由
$router->group([
'domain' => $backendUrl,
'namespace' => $this->backendNamespace],
function ($router) {
require app_path('Http/routes-backend.php');
}
);
break;
default:
// 前端路由
$router->group([
'domain' => $frontendUrl,
'namespace' => $this->frontendNamespace],
function ($router) {
require app_path('Http/routes-frontend.php');
}
);
break;
}
}
}
此時(shí)只需要在不同的控制器中建立路由就 Ok了。
以上這篇laravel實(shí)現(xiàn)前后臺路由分離的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Laravel 5.5 異常處理 & 錯(cuò)誤日志的解決
今天小編就為大家分享一篇Laravel 5.5 異常處理 & 錯(cuò)誤日志的解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP利用超級全局變量$_GET來接收表單數(shù)據(jù)的實(shí)例
下面小編就為大家?guī)硪黄狿HP利用超級全局變量$_GET來接收表單數(shù)據(jù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Yii使用DeleteAll連表刪除出現(xiàn)報(bào)錯(cuò)問題的解決方法
這篇文章主要介紹了Yii使用DeleteAll連表刪除出現(xiàn)報(bào)錯(cuò)問題的解決方法,分析了相關(guān)的SQL語句及出現(xiàn)錯(cuò)誤的原因與改正方法,需要的朋友可以參考下2016-07-07
PHP中error_reporting函數(shù)用法詳細(xì)介紹
這篇文章主要介紹了PHP中error_reporting函數(shù)用法詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-06-06
在PHP中實(shí)現(xiàn)使用Guzzle執(zhí)行POST和GET請求
今天小編就為大家分享一篇在PHP中實(shí)現(xiàn)使用Guzzle執(zhí)行POST和GET請求,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
淺談laravel-admin form中的數(shù)據(jù),在提交后,保存前,獲取并進(jìn)行編輯
今天小編就為大家分享一篇淺談laravel-admin form中的數(shù)據(jù),在提交后,保存前,獲取并進(jìn)行編輯,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Joomla調(diào)用系統(tǒng)自帶編輯器的實(shí)現(xiàn)方法
這篇文章主要介紹了Joomla調(diào)用系統(tǒng)自帶編輯器的實(shí)現(xiàn)方法,實(shí)例分析了Joomla調(diào)用系統(tǒng)自帶編輯器的具體步驟、相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2016-05-05
thinkphp普通查詢與表達(dá)式查詢實(shí)例分析
這篇文章主要介紹了thinkphp普通查詢與表達(dá)式查詢,以實(shí)例形式較為詳細(xì)的分析了thinkphp中的普通查詢與表達(dá)式查詢具體用法,包含普通查詢的字符串方式與數(shù)組方式以及表達(dá)式查詢中的各種常用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
laravel 5.5 關(guān)閉token的3種實(shí)現(xiàn)方式
今天小編就為大家分享一篇laravel 5.5 關(guān)閉token的3種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

