第一步
在app\http\Controllers文件夹里面创建我们要存放前端和后端或者接口的文件夹
列如: Home(前端) Admin(后端) App(接口) 文件夹
第二步
修改app\http\providers\RouteServiceProvider.php
在protected $namespace = 'App\Http\Controllers’后面添加
/**
- 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 the routes for the application.
- @return void
- 修改map方法
*/
public function map()
{
//this−>mapApiRoutes();//this->mapApiRoutes();
//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();
}
}
添加新方法
/**
- 管理后台
*/
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’));
}
第三步
在config下创建文件route.php,并在里面添加:
第四步
在routes目录下创建 api.php ,home.php 和 author.php 路由
第五步
分别在app\Http\Controllers\你创建的目录下创建方法
第六步
在刚刚创建的路由文件(routes/api.php)创建路由
Route::get(’/’, ‘IndexController@index’);
第七步
测试