benycode/slim-annotation-router 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

benycode/slim-annotation-router

最新稳定版本:v1.0.0

Composer 安装命令:

composer require benycode/slim-annotation-router

包简介

Slim Annotation Router for Slim 4

README 文档

README

Latest Version on Packagist Latest PHP Version License Build Status Coverages

Annotation Router for Slim 4.x

Installation

It's recommended using Composer to install Slim Annotation Router.

$ composer require "nicc0/slim-annotation-router"

This will install Slim Annotation Router and all required dependencies. Remember Slim 4.x requires PHP 7.1 or newer.

Usage

$factory = new DecoratedResponseFactory( new ResponseFactory(), new StreamFactory() );
$resolver = new CallableResolver();

$controllerPath = './app/controllers/';

$collector = new AnnotationRouteCollector( $factory, $resolver, $container );
$collector->setDefaultControllersPath( $controllersPath );
$collector->collectRoutes();

$app = new App( $factory, $container, $resolver, $collector );

Creating Routes by Annotation

/**
 * Class ExampleController
 *
 * @RoutePrefix("/example")
 */
class ExampleController
{
    /**
     * @Route("/hello", methods={"GET"}, name="example.hello")
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function hello(): ResponseInterface
    {
        $response = new Response();
        $response->getBody()->write('Hello world!');

        return $response;
    }
}

By opening the url http://your_site_url/example/hello, you should see "Hello world!".

Adding Middlewares to contoller by Annotation

To add Middleware to Controller or Action use @Middleware("") annotation, which pass the name of Middleware. It is important to know that the name we pass must be defined in the Container. Name passes as the third parameter in the AnnotationRouteCollector constructor. It is also important that the added Middleware must implements of MiddlewareInterface otherwise Middleware will not be added to the Route.

There is also possibility to add more than one Middleware to Controller or Action.

For example, we have to add AuthMiddleware to controller. Firstly we have to define AuthMiddleware in Container.

$container->set('authMiddleware', function() use ($container) {
    return new AuthContainer(container);
})

If Middleware exists in our Container, now we can use middleware annotation by adding @Middleware("authMiddleware") to controller.

/**
 * @RoutePrefix("/example")
 * @Middleware("authMiddleware")
 */
class ExampleController
{
    /**
     * @Route("/hello", methods={"GET"}, name="example.hello")
     */
    public function hello(): ResponseInterface
    {
        ...
    }
}

Tests

To execute the test suite, you'll need to install all development dependencies.

git clone https://github.com/Nicc0/Slim-Annotation-Router
composer install
composer test

License

The Slim Annotation Router is licensed under the MIT license. See License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-30