joanrodas/plubo-routes

让WordPress路由变得简单。

v0.6.1 2023-12-28 16:04 UTC

README

Plubo Routes

GitHub stars Code Climate maintainability

让WordPress路由变得简单。

✔️ 无需手动编写重写规则和标签
✔️ 路由更改时自动刷新重写规则
✔️ 自定义重定向和动作路由
✔️ 通过钩子轻松扩展
✔️ 与Sage 10易用


开始使用

composer require joanrodas/plubo-routes

您还可以将Plubo Routes作为独立的WordPress插件安装,只需下载zip文件并将其放入插件文件夹。


阅读文档


添加新路由

存在不同类型的路由


如何添加新路由

您可以使用以下过滤器添加新路由

PluboRoutes\RoutesProcessor::init();

add_filter( 'plubo/routes', array($this, 'add_routes') );
public function add_routes( $routes ) {
    //Your routes
    return $routes;
}

基本路由

基本路由需要3个参数

示例

use PluboRoutes\Route\Route;

add_filter( 'plubo/routes', array($this, 'add_routes') );
public function add_routes( $routes ) {
    $routes[] = new Route('clients/list', 'template_name');

    //SAGE 10 example
    $routes[] = new Route(
        'dashboard/{subpage:slug}',
        function($matches) {
            $subpage = 'dashboard/' . $matches['subpage'];
            return locate_template( app('sage.finder')->locate($subpage) );
        },
        [
            'name' => 'my-route'
        ]
    );
    return $routes;
}

可用语法

您可以使用任何可用类型的格式 {变量名:类型}

  • 数字(仅数字)
  • 单词(仅a-Z)
  • 别名(有效的WordPress别名)
  • 日期(yyyy-mm-dd日期)
  • 年(4位数字)
  • 月(01-12)
  • 日(01-31)
  • 数字(单个数字0-9)
  • JWT(JWT令牌)
  • IP(IPv4)

您还可以使用自定义正则表达式模式,格式为 {变量名:正则表达式},例如 {作者:([a-z0-9-]+)}


更改通用模板路径

默认情况下,Plubo Routes将在您的主题内搜索模板,但您可以使用钩子更改默认路径。

如果您使用Sage 10,可以添加类似以下内容

add_filter( 'plubo/template', function($template) {
    return app('sage.finder')->locate($template);
});

自定义动作

命名路由提供了一个钩子来执行您的自定义动作

add_action('plubo/route_{route_name}', function($matches) {
    //Do something
});

贡献

contributions welcome GitHub issues GitHub license

请随意为项目做出贡献,提出改进建议、报告错误和编码。