mbezhanov/silex-route-annotations 问题修复 & 功能扩展

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

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

mbezhanov/silex-route-annotations

Composer 安装命令:

composer require mbezhanov/silex-route-annotations

包简介

A service provider that allows you to use @Route annotations in your Silex applications.

README 文档

README

A service provider that allows you to use @Route annotations in your Silex applications, in order to define routes directly in your Controllers.

Installation

Install the library through Composer:

composer require mbezhanov/silex-route-annotations

Registering

To enable @Route annotations, register the Service Provider with your Application:

<?php 

use Bezhanov\Silex\Routing\RouteAnnotationsProvider;
use Silex\Application;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$app = new Application();

$app->register(new RouteAnnotationsProvider(), [
    // it is required to specify the full path, where your Controllers reside
    'routing.controller_dir' => __DIR__ . '/../src/App/Controllers',
    // not required, but highly recommended to specify a Cache Adapter, in order to use caching
    'routing.cache_adapter' => new FilesystemAdapter('routing', 0, __DIR__ . '/../var/cache'),
]);

Basic Usage

Registering the Service Provider with the Application allows you to use @Route annotations, identical to the ones from SensioFrameworkExtraBundle / Symfony in your Controllers.

<?php

namespace Bezhanov\Silex\Routing\Tests\Fixtures\AnnotatedClasses;

use Bezhanov\Silex\Routing\Route;

/**
 * @Route("/foo")
 */
class FooController
{
    /**
     * @Route("/bar/{lorem}")
     */
    public function barAction($lorem = 'ipsum')
    {
        // ...
    }
}

Service Controllers

As your application grows, you may find yourself utilizing the Service Controllers mechanism that Silex provides, in order to inject external dependencies into your Controllers. The library gets you covered in such cases, as it allows you to have your Controllers instantiated through the Service Container out of the box:

<?php

// Service Controller definition:
$app['foo.bar_baz'] = function ($app) {
    return new App\Controller\FooController($app['some_other_service']);
};

// Controller example:

namespace App\Controller;

use Bezhanov\Silex\Routing\Route;
use Symfony\Component\HttpFoundation\Response;

/**
 * @Route(service="foo.bar_baz") 
 */
class FooController
{
    private $service;
    
    public function __construct(SomeOtherService $service)
    {
        $this->service = $service;
    }
    
    /**
     * @Route("/foo")
     */
    public function fooAction()
    {
        $result = $this->service->doSomething();
        return new Response($result, Response::HTTP_OK);
    }
}

You can also omit the service option entirely from your @Route declaration, as long as your service ID matches the ID derived from your fully-qualified class name (FQCN), e.g.

<?php

$app['app.controller.manufacturer_controller'] = function (Application $app) {
    return new App\Controller\FooController($app['some_other_service']);
};

Here, the Controller will be automatically instantiated through the Service Container, and you don't have to explicitly specify a service attribute in your class-level @Route annotation.

Contributing

This library is in its early stages of development. All contributions are welcome. Before opening PRs, make sure that all tests are passing, and that code coverage is satisfactory:

phpunit tests --coverage-html coverage

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-30