calen / router
Laravel Ratchet 路由器
Requires
- php: ^7.0
- cboden/ratchet: ^0.3.4
This package is not auto-updated.
Last update: 2024-09-18 19:35:31 UTC
README
该路由器提供了进行WebSocket URL路由的好方法。该库依赖于cboden/ratchet,这是使用的WebSocket服务器,但您也可以为其他WebSocket服务器编写相同的代码。
该库基于JSON消息中给出的path
字段进行工作。所有配置都通过发布的router.php文件完成。
该库处理中间件,类似于Laravel 5 URL路由中的分组命名空间前缀。
安装
在您的L5项目中需要Calen/router。我用PHP7编写的它,因此它依赖于它。
composer require calen/router
然后在L5 config/app.php 中的提供者中添加服务提供者
'providers' => [
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
// ...
Calen\Router\RatchetRouterServiceProvider::class,
],
一旦添加了服务提供者,您可以通过运行artisan发布配置文件
php artisan vendor:publish
这将添加配置文件 router.php 到配置存储库中。
配置
您需要配置库以便其正确运行。配置文件发布如下
return [
'middlewares' => [
// 'test' => App\WS\AuthMiddleware::class,
],
'routes' => function ($routes) {
$routes->group(['namespace' => 'App\Http\Controllers'], function ($routes) {
$routes->route('/', 'HomeController@index');
});
},
];
中间件
中间件看起来与L5的相同。如果您想在某些路由上提供中间件,可以在group
函数给出的数组中添加middleware
键。
中间件必须实现Calen\Router\Routing\Middleware\Middleware接口。
例如
$routes->group(
[
'namespace' => 'App\Http\Controllers',
'middleware' => 'dummy',
'prefix' => 'CalenApp',
], function ($routes) {
// My routes
});
我们将中间件dummy添加到闭包中给出的所有路由。
如L5,中间件在config/router.php文件中注册,对应于类的名称,如下所示
return [
'middlewares' => [
'dummy' => App\WS\Dummyddleware::class,
],
'routes' => function ($routes) {
$routes->group(['namespace' => 'App\Http\Controllers'], function ($routes) {
$routes->route('/', 'HomeController@index');
});
},
];
####注意: 如果中间件未注册,则不会处理
Packagist
我的packagist仓库在这里:https://packagist.org.cn/packages/calen/router