Laravel 路由版本 文件夹
本文作者LockieDeng,PHP工程师,致力于倒腾各种前沿技术,欢迎交流:
dengmuuming@163.com
0、目的
1、路由定义
app/Providers/RouteServiceProvider.php
- map()方法新增自己的路由方法,如:$this->mapChatV1();
- 源码
<?php
/**
* 文件路径 app/Providers/RouteServiceProvider.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();
// 聊天接口V1版本
$this->mapChatV1();
//
}
/**
* 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 "chat" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapChatV1()
{
Route::prefix('chat/v1')
// ->middleware('chat')
->namespace($this->namespace)
->group(base_path('routes/chat/v1.php'));
}
}
- 实现mapChatV1()方法
2、路由实现
routes/chat/v1.php
- 创建文件routes/chat/v1.php
- 写入路由
- 源码
<?php
/**
* 文件路径 routes/chat/v1.php
**/
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
//Route::get('depapier', '\App\Http\Controllers\Chat\V1\Test\Depapier@show'); // 全路径写法
Route::get('depapier', 'Test\Depapier@show'); // 简单写法
3、控制器
app/Http/Controllers/Chat/V1/Test/Depapier.php
- 源码
<?php
/**
* 文件路径 app/Http/Controllers/Chat/V1/Test/Depapier.php
**/
namespace App\Http\Controllers\Chat\V1\Test;
use App\Http\Controllers\Controller;
class Depapier extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
dd(123123);
die();
}
public function show()
{
dd(321);
die();
}
}
4、路由访问
http://laraveltest.com/chat/v1/depapier