在实行前后台分离时,使用artisan时出现Undefined index: HTTP_HOST

<?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 $authorNamespace = 'App\Http\Controllers\Author';

protected $homeNamespace = 'App\Http\Controllers\Home';

protected $apiNamespace = 'App\Http\Controllers\Api';

/**
* 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.api_url') == $sld_prefix){
$this->mapApiRoutes();
}elseif(config('route.author_url') == $sld_prefix){
$this->mapAuthorRoutes();
}elseif(config('route.home_url') == $sld_prefix){
$this->mapHomeRoutes();
}

}

/**
* 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'));
}


/**
* 管理后台
*/
protected function mapApiRoutes()
{
Route::middleware('web')
->namespace($this->apiNamespace)
->group(base_path('routes/api.php'));
}

/**
* 管理作者
*/
protected function mapAuthorRoutes()
{
Route::middleware('web')
->namespace($this->authorNamespace)
->group(base_path('routes/author.php'));
}

/**
* 管理前台
*/
protected function mapHomeRoutes()
{
Route::middleware('web')
->namespace($this->homeNamespace)
->group(base_path('routes/home.php'));
}

}

2 条回复

  1. 不建议这么使用,之后又用了group路由分组,最终版本是 ,后台用的laraveladmin,后台路由自带,api写到了api路由,web写前台路由。相对来说,我认为最终版本还是比较友好的。

  2. 以解决 public function map() { //$this->mapApiRoutes();

       $this->mapWebRoutes();
    
        if (!isset($_SERVER['HTTP_HOST'])){
            $sld_prefix='www';
        }else{
            $sld_prefix = explode('.',$_SERVER['HTTP_HOST'])[0];
        }
       if(config('route.api_url') == $sld_prefix){
            $this->mapApiRoutes();
        }elseif(config('route.author_url') == $sld_prefix){
            $this->mapAuthorRoutes();
        }elseif(config('route.home_url') == $sld_prefix){
           $this->mapHomeRoutes();
       }
    
    }

登录后才能进行评论,立即登录?