Laravel 5.4前后台分离,通过不同的二级域名访问方法

2020-10-20 14:59:17 浏览数 (1)

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

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

第二步:修改apphttpprovidersRouteServiceProvider.php

代码语言:javascript复制
<?php

namespace AppProviders;

use IlluminateSupportFacadesRoute;
use IlluminateFoundationSupportProvidersRouteServiceProvider 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 = 'AppHttpControllers';
 protected $homeNamespace = 'AppHttpControllersHome';//PC端
 protected $adminNamespace = 'AppHttpControllersAdmin';//管理后台

 /**
 * 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 路由

第四步:分别在appHttpControllersAdmin和appHttpControllersHome

代码语言:javascript复制
<?php
namespace AppHttpControllersAdmin;
use AppHttpControllersController;

class AdminController extends Controller
{
 public function index()
 {
 echo "this is admin";
 }
}
代码语言:javascript复制
<?php
namespace AppHttpControllersHome;
use AppHttpControllersController;

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 ‘AppHttpControllersController’ not found

错误二:Class AppHttpControllersIndexController does not exist

解决方法:

在PHPstorm Terminal控制台输入“composer dump-autoload”

因为laravel是用composer来加载类,不是命令创建的类要更新autoload。

如果没有使用PHPstorm编辑器的话,我们需要在本地安装composer,然后cmd以管理员运行,进入到项目的根目录执行“composer dump-autoload”

以上这篇Laravel 5.4前后台分离,通过不同的二级域名访问方法就是小编分享给大家的全部内容了,希望能给大家一个参考。

0 人点赞