定制 squidit/slim-attribute-router 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

squidit/slim-attribute-router

最新稳定版本:v0.1.2

Composer 安装命令:

composer require squidit/slim-attribute-router

包简介

Slim (v4) Framework - Attribute Router

README 文档

README

Attribute Action/Controller router

This package allows you to add routes to your slim4 (https://www.slimframework.com) application using attributes in your action classes.

Features

  • Route Method support
  • Route name support

Attribute signature

#[Route({route}[[, {methods}], {routeName}])]

Parameter example value Description
{route} '/hello/{name}' (string) The route pattern
{methods} ['GET', 'POST'] (array) The allowed HTTP request methods
{routeName} 'helloRoute' (string) The name of the route

Adding Attribute to an ActionController

If you want to add a route using attributes you can accomplish this by adding a #[Route({route}[[, {methods}], {routeName}])] route tag to your class method. please see examples:

Example
web address test 1: https://server.name/test1/http/202

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;


class StatusCodeAction
{
    #[Route('/test1/http/{statusCode:[1-5]{1}\d{2}}',['GET', 'POST', 'PUT', 'DELETE'], 'test_http_statuscode')]
    public function __invoke(Request $request, Response $response, array $args): Response
    {
        // action/controller code
        return $response;
    }
}

Examples without a 'name' parameter
web address test 2: https://server.name/test2/http/202
web address test 3: https://server.name/test3/http/202

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class StatusCodeAction
{
    #[Route('/test2/http/{statusCode:[1-5]{1}\d{2}}',['POST'])]
    public function testMethod2(Request $request, Response $response, array $args): Response
    {
        // action/controller code
        return $response;
    }
	
    #[Route('/test3/http/{statusCode:[1-5]{1}\d{2}}',['DELETE'])]
    public function testMethod3(Request $request, Response $response, array $args): Response
    {
        // action/controller code
        return $response;
    }
}
  • The methods parameter is required when writing the Route attribute
  • The path to your ActionController files needs to be specified when instantiating the attribute router

Installation

This package can be installed using Composer
Navigate into your project's root directory and execute the bash command shown below

composer require squidit/slim-attribute-router

Enabling the Attribute Router

Our attribute router extends slims default RouteCollector, so we can just instantiate our attribute router and pass it on to our AppFactory

<?php
use SquidIT\Slim\Routing\AttributeRouteCollector;
use Slim\Factory\AppFactory;

// create AttributeRouteCollector
$attributeRouteCollector = new AttributeRouteCollector([
    'path' => [
        '/path/to/ActionControllers',
        '/different/path/to/ActionControllers/if/needed',
    ]],
	AppFactory::determineResponseFactory(),
	new CallableResolver($container) // pass in your DI container if you are using a container
);

// Instantiate the app
AppFactory::setRouteCollector($attributeRouteCollector);
$app = AppFactory::create();
$app->run();

统计信息

  • 总下载量: 45
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-19