elfcorreia/routing

dev-main 2021-02-01 14:21 UTC

This package is auto-updated.

Last update: 2024-09-29 05:49:02 UTC


README

这是一个引入了最小化功能集的库,用于在PHP应用程序中处理路由。

安装

使用 composer require elfcorreia/routing @dev 安装

快速开始

<?php

use function routing\route;
use function routing\debug;

route('/');
route('/posts');
route('/posts/{name:str}');

debug();

用法

这个库中的所有函数都在 routing 命名空间中。因此,为了使用它,您必须 导入一个函数 或通过其限定名称调用它。

添加路由

使用必需的 path、可选的 callback 和可选的 name 调用函数 route 来添加一个 route。示例

route('/');
route('/posts');
route('/posts/{name:str}');
route('/posts/{name:str}/comments');

如果未指定任何回调,则函数 routing/not_implemented_yet_handler 是默认的 callback。为了做到这一点,您必须传递一个 回调/callable 变量。

// a function name
route('/', 'index_view'); 
// an anonimous function 
route('/posts', function () { /**/ });
// an arrow function
route('/posts/{name:str}', fn ($name) => { /**/ });
// an static class method
route('/posts/{name:str}/comments', 'PostsController::comments');
// an instance method
route('/posts/{name:str}/comments', [new MyClass(), 'comments']);

路径参数指定在路径中为 '{name:type}',其中名称必须是一个有效的PHP标识符,类型应该是有效的路径类型。