web.php中
Route::group(array('prefix'=>'api'),function(){
Route::get('test', 'Api\UserController@test');
Route::group(array('prefix'=>'user'),function() {
Route::get('tests', 'Api\UserController@tests');
});
Route::group(array('prefix'=>'test'),function() {
Route::get('index', 'Api\TestController@index');
//Route::get('edit/{id}', 'Api\TestController@edit');
Route::get('edit/{id}/{name}', 'Api\TestController@edit');
});
});
或者
Route::prefix('api')->namespace('Api')->group(function () {
Route::get('test', 'UserController@test');
Route::group(array('prefix'=>'user'),function() {
Route::get('tests', 'UserController@tests');
});
Route::group(array('prefix'=>'test'),function() {
Route::get('index', 'TestController@index');
//Route::get('edit/{id}', 'TestController@edit');
Route::get('edit/{id}/{name}', 'TestController@edit');
});
});
直接放个控制器的方法吧
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class TestController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return 'index';
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//return $id;
return 'id是:' . $id . '<br>name是:' . $name;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
好了 浏览器访问
/api/test/edit/222
/api/test/index
多层路由嵌套使用,这样就好继承两个中间件了
Route::group(['middleware' => 'AppAllSingleLogin'], function ($router) {
Route::group(['middleware' => 'AppSingleLoginOrNot', 'namespace' => 'Api'], function ($router) {
Route::get('recharge/updateStatus', 'RechargeController@updateStatus');
});
});
如果要比如app所有接口都使用某个中间件,那就简单了
直接在 Kernel.php的
$middlewareGroups 添加 所对应的分组就可以了
'api' => [
\App\Http\Middleware\AppRequestLog::class,
// \App\Http\Middleware\AppAllSingleLogin::class,
'throttle:1000,1',
'bindings',
],
需要整个模块的路由
app/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';
/**
* 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();
$this->mapBusinessHomeRoutes();
$this->mapAdminRoutes();
$this->mapBackEndRoutes();
$this->mapMemberRoutes();
//
}
/**
* 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'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapBusinessHomeRoutes()
{
Route::prefix('home')
->middleware('web')
->namespace($this->namespace."\Business")
->group(base_path('routes/business.php'));
}
//
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
//
protected function mapAdminRoutes()
{
Route::prefix('adm')
->middleware('web')
->namespace($this->namespace."\Admin")
->group(base_path('routes/admin.php'));
}
protected function mapBackEndRoutes()
{
Route::namespace($this->namespace)
->middleware('backend')
->group(base_path('routes/backend.php'));
}
protected function mapMemberRoutes()
{
Route::prefix('member')
->middleware('web')
->namespace($this->namespace."\Member")
->group(base_path('routes/member.php'));
}
}
例如admin模块修改为adm 直接访问 域名/adm+接口方法就可以使用了