gaetanroger/slim-routes-loader 问题修复 & 功能扩展

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

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

gaetanroger/slim-routes-loader

Composer 安装命令:

composer require gaetanroger/slim-routes-loader

包简介

Register your Slim routes using a simple config file.

README 文档

README

Build Status

When using Slim PHP Framework, you need to register your routes. This library allows you to do so via a simple config file.

How to install

Using composer is the easiest way.

composer require gaetanroger/slim-routes-loader

You can always get the sources from this repo and install it manually in your project.

How to use

For more info about the syntax to follow, see Syntax and rules.

Regular PHP array format

Write your routes using a regular PHP associative array, then use the Loader class to import it.

$routes = [
    'pattern' => '',
    'routes'  => [
        [
            'pattern'  => '/',
            'method'   => 'GET',
            'callable' => 'myCallable',
            'name'     => 'optianalName',
        ],
    ],
];
$slim = new \Slim\App();

$loader = new Gaetanroger\SlimRoutesLoader\Loader($routes);
$loader->load($slim); // or $loader($slim)

If you want to store your routes in another file, simply include them using a regular PHP require.

$routes = require __DIR__ '/myRoutes.php';
$slim = new \Slim\App();

$loader = new \Gaetanroger\SlimRoutesLoader\Loader($routes);
$loader($slim); // or $loader->load($slim)

Json format

If you prefer to write your routes as Json, simply use the JsonLoader.

{
    "pattern": "",
    "routes": [
        {
            "pattern": "/",
            "method": "GET",
            "callable": "testCallable",
            "name": "testName"
        }
    ]
}
$slim = new \Slim\App();

$loader = new \Gaetanroger\SlimRoutesLoader\JsonLoader($json);
$loader($slim); // or $loader->load($slim)

The $json variable can contain a json string or the path to a json file containing the routes.

Yaml format

If you prefer to write your routes as Yaml, simply use the YmlLoader.

pattern:
routes:
  - pattern: /
    method: GET
    callable: testCallable
    name: testName
$slim = new \Slim\App();

$loader = new \Gaetanroger\SlimRoutesLoader\YmlLoader($yml);
$loader($slim); // or $loader->load($slim)

The $yml variable can contain a Yaml string or the path to a Yaml file containing

Syntax and rules

No matter what format you choose to use, the loaders require a certain syntax.

Please see the Slim documentation to get more info about specific points.

Route syntax

  • pattern: the route pattern
  • method: the HTTP method the route is waiting for (GET, POST, etc.)
  • callable: the function/method/invokable to be called when the route is reached
  • name: (optional) the name of the route

Group syntax

  • pattern: the group pattern
  • routes: an array containing other groups or routes

General rules

  • The top element must be a group (usually with an empty pattern unless you want to prefix all your routes)
  • A pattern can be left empty.

Examples

PHP array syntax

$routes = [
    'pattern' => '',
    'routes'  => [
        [
            'pattern'  => '/one',
            'method'   => 'GET',
            'callable' => 'testCallable1',
            'name'     => 'testName1',
        ],
        [
            'pattern'  => '/two',
            'method'   => 'POST',
            'callable' => 'testCallable2',
            'name'     => 'testName2',
        ],
        [
            'pattern' => '/group',
            'routes'  => [
                [
                    'pattern'  => '/one',
                    'method'   => 'GET',
                    'callable' => 'testCallable1',
                    'name'     => 'testName1',
                ],
                [
                    'pattern'  => '/two',
                    'method'   => 'POST',
                    'callable' => 'testCallable2',
                    'name'     => 'testName2',
                ],
            ],
        ],
    ],
];

Json syntax

{
    "pattern": "",
    "routes": [
        {
            "pattern": "/one",
            "method": "GET",
            "callable": "testCallable1",
            "name": "testName1"
        },
        {
            "pattern": "/two",
            "method": "POST",
            "callable": "testCallable2",
            "name": "testName2"
        },
        {
            "pattern": "/group",
            "routes": [
                {
                    "pattern": "/one",
                    "method": "GET",
                    "callable": "testCallable1",
                    "name": "testName1"
                },
                {
                    "pattern": "/two",
                    "method": "POST",
                    "callable": "testCallable2",
                    "name": "testName2"
                }
            ]
        }
    ]
}

Yaml syntax

pattern:
routes:
  - pattern: /one
    method: GET
    callable: testCallable1
    name: testName1
  - pattern: /two
    method: POST
    callable: testCallable2
    name: testName2
  - pattern: /group
    routes:
      - pattern: /one
        method: GET
        callable: testCallable1
        name: testName1
      - pattern: /two
        method: POST
        callable: testCallable2
        name: testName2';

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固