icanboogie/bind-routing

将 icanboogie/routing 绑定到 ICanBoogie。

5.0 2021-05-31 22:42 UTC

This package is auto-updated.

Last update: 2024-08-31 00:26:20 UTC


README

Packagist Code Quality Code Coverage Downloads

icanboogie/bind-routing 包将 ICanBoogie/Routing 绑定到 ICanBoogie。它提供配置路由和响应者的基础设施,一个用于从对象获取 URL 的特质,以及列出路由和命令。

安装

composer require icanboogie/bind-routing

使用属性定义路由

定义路由的最简单方法是使用诸如 RouteGet 等属性来标记您的控制器和操作。使用这些标记之一将触发将控制器注册为服务(如果尚未注册),并将其标记为 action_responderaction_alias

以下示例演示了如何使用 Route 属性在类级别上指定控制器所有操作的的前缀。使用 Get 和 [Post][] 属性来标记操作。如果未定义,则从控制器类和方法名称推断操作。

<?php

namespace App\Presentation\HTTP

use ICanBoogie\Accessor\AccessorTrait;use ICanBoogie\Binding\Routing\Attribute\Get;use ICanBoogie\Binding\Routing\Attribute\Route;use ICanBoogie\Routing\ControllerAbstract;

#[Route('/skills')]
final SkillController extends ControllerAbstract
{
    use AccessorTrait;

    // This will create a 'GET /skills' route with 'skills:list' action
    #[Get]
    private function list(): void
    {
        // …
    }

    // This will create a 'GET /skills/:slug' route with 'skills:show' action
    #[Get('/:slug')]
    private function show(string $slug): void
    {
        // …
    }

    // This will create a 'POST /skills' route with 'skills:create' action
    #[Post]
    private function create(): void
    {
        // …
    }
}

使用 use_attributes() 方法使用属性配置构建器

<?php
// app/all/config/routes.php

namespace App;

use ICanBoogie\Binding\Routing\ConfigBuilder;

return fn(ConfigBuilder $config) => $config->use_attributes();

使用配置片段定义路由

或者,您可以使用 routes 配置片段手动配置路由,但您必须注册服务并使用 action_responderaction_alias 标记它。

以下示例演示了如何定义路由,资源路由。将 articles:show 路由的模式覆盖为使用 yearmonthslug

<?php

// config/routes.php

namespace App;

use ICanBoogie\Binding\Routing\ConfigBuilder;
use ICanBoogie\Routing\RouteMaker;

return fn(ConfigBuilder $config) => $config
    ->route('/', 'page:home')
    ->resource('articles', new Make\Options(
        basics: [
            RouteMaker::ACTION_SHOW => new Make\Basics('/articles/:year-:month-:slug.html')
        ]
    ));

将路由与控制器匹配

路由不知道要使用哪个控制器,要匹配路由与控制器,您需要标记控制器以支持的操作。

以下示例演示了如何配置 ArticleControler 以处理 articles:showarticles:list 操作。

services:
  _defaults:
    autowire: true

  App\Presentation\HTTP\Controller\ArticleController:
      shared: false
      tags:
      - { name: action_responder }
      - { name: action_alias, action: 'articles:list' }
      - { name: action_alias, action: 'articles:show' }

获取路由配置

以下代码演示了如何从 routes 配置中获取路由提供程序

<?php

namespace ICanBoogie;

/** @var Application $app */

$routes = $app->config_for_class(Routing\RouteProvider::class);

持续集成

项目通过 GitHub actions 持续测试。

Tests Static Analysis Code Style

行为准则

本项目遵守 贡献者行为准则。通过参与本项目及其社区,您应遵守此准则。

贡献

请参阅 CONTRIBUTING 以获取详细信息。

许可

icanboogie/bind-routingBSD-3-Clause 许可下发布。