enstart/croute

使用控制器文档块中的注解创建路由

dev-develop 2017-10-09 20:15 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:42:56 UTC


README

此扩展仍在开发中,不应在生产环境中使用,除非至少有一个标记版本。

使用控制器文档块中的文档块注解创建路由。您仍然可以使用创建路由的默认方式。

依赖

  • enstart/core 版本 0.2+
  • PHP 7.0+

安装

composer require enstart/croute

配置

// Settings
'croute' => [
    // Enable the parser
    'enabled'     => true,

    // Use the cached file, if it exists
    'use_cache'   => false,

    // Path to the cache
    'cache'       => '/path/to/cache/folder',

    'controllers' => [
        // Namespace => path
        'App\Controllers\Api' => __DIR__ . '/app/Controllers/Api',
    ],
],

// Register the service provider
'providers' => [
    ...
    'Enstart\Ext\Croute\ServiceProvider',
],

访问扩展

// Get a copy of the instance
$croute = $app->container->make('Enstart\Ext\Croute\Parser');

// or through the alias:
$app->croute

// or through dependency injection (if you type hint it in your constructor)
use Enstart\Ext\Croute\Parser;

注解

以下是一个简单的注解方法

class TestController
{
    /**
     * Get list of items
     *
     * @route GET /items
     *
     * @return json
     */
    public function list()
    {
        return $this->makeJsonEntity(true, ['list of items']);
    }
}

这将使用GET方法注册路由/items

整个类的路由前缀

/**
 * @routePrefix /test
 */
class TestController
{
    /**
     * Get list of items
     *
     * @route GET /items
     *
     * @return json
     */
    public function list()
    {
        return $this->makeJsonEntity(true, ['list of items']);
    }
}

上述代码将注册路由/test/items。上述类中所有方法路由都将具有/test前缀。

可选的路由设置

就像普通路由器一样,您可以添加过滤器、命名路由等。

/**
 * Get list of items
 *
 * @route GET /items
 * @routeName list-items
 * @before filter1|filter2|...
 * @after  filter1|filter2|...
 *
 * @return json
 */
public function list()
{
    return $this->makeJsonEntity(true, ['list of items']);
}

@before@after注解也可以用于类的注解。

缓存

...更多信息即将到来。