zloynick / joole-components-routing

8.1.3.2 2022-05-03 22:30 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:12:15 UTC


README

此组件允许您为项目配置路由。

入门指南

  • 通过composer安装此依赖项:composer install zloynick/joole-components-routing

配置

在joole.php配置文件中将以下内容添加到components中


'components' => [
        ...,
        [
            'name' => 'router',
            'class' => \joole\components\routing\RoutingComponent::class,
            // Component options
            'options' => [
                // Routes path
                'routes' => __DIR__.'/routes/',
            ],
        ],
        ...,
    ],

使用方法

在组件配置中指定的文件夹中创建您的路由

----------- routes.php -----------

...
// Closure using.
BaseRouter::register('route.test', '/my/action/:name/:and_id', function(string $name, int $and_id):\joole\framework\http\Response{
          return response();
      }
);
// Controller using.
BaseRouter::register('route.test', '/my/action/:name/:and_id', ['\app\controllers\DefaultController::class', 'index']);
// OR
BaseRouter::register('route.test', '/my/action/:name/:and_id', '\app\controllers\DefaultController::class@index');
...

请求验证

使用Validator 创建一个请求参数验证类。

示例


...
BaseRouter::register('route.test', '/my/action/:name/:and_id', function(string $name, int $and_id):\joole\framework\http\Response{
          return response();
      }
)->withValidators(YourValidator1::class, ..., new Validator2());
...