humanmade/hm-rewrite
Composer 安装命令:
composer require humanmade/hm-rewrite
包简介
HM_Rewrite is a wrapper for the WordPress WP Rewrite system. http://hmn.md/wordpress-rewrite-rules-hm-core-style/
README 文档
README
HM_Rewrite and HM_Rewrite_Rule are wrappers for the WordPress rewrite / wp_query system.
The goal of HM_Rewrite and associated fuctions / classes is to make it very easy to add new routing points with new pages (as in dynamic pages, post_type_archive etc). It basically wraps a few tasks into a nice API. Everything (almost) you need for setting up a new routing page can be done all at once, relying heavily on PHP Closures. It essentially wraps adding to the rewrite_rules, adding your template file to template_redirect, wp_title hook, body_class hook, parse_query hook etc. Also also provides some callbacks for conveniance. Each rewrite rule is an instance of HM_Rewrite_Rule. Here you add the regex / wp_query vars and any other options for the "page". For example a callback function to parse_request to add additional query vars, or a callback * body_class. There is also a wrapper function for all of this in one call hm_add_rewrite_rule(). hm_add_rewrite_rule() is generally the recommended interface, you can interact with the underlying objects for more advanced stuff (and also tacking onto other rewrite rules)Simple use case example:
hm_add_rewrite_rule( array( 'regex' => '^users/([^/]+)/?', 'query' => 'author_name=$matches[1]', 'template' => 'user-archive.php', 'body_class_callback' => function( $classes ) { $classes[] = 'user-archive'; $classes[] = 'user-' . get_query_var( 'author_name' ); return $classes; }, 'title_callback' => function( $title, $seperator ) { return get_query_var( 'author_name' ) . ' ' . $seperator . ' ' . $title; } ) );
A more advanced example using more callbacks:
hm_add_rewrite_rule( array( 'regex' => '^reviews/([^/]+)/?', // a review category page 'query' => 'review_category=$matches[1]', 'template' => 'review-category.php', 'request_callback' => function( WP $wp ) { // if the review category is "laptops" then only show items in draft if ( $wp->query_vars['review_category'] == 'laptops' ) $wp->query_vars['post_status'] = 'draft'; }, 'query_callback' => function( WP_Query $query ) { //overwrite is_home because WordPress gets it wrong here $query->is_home = false; }, 'body_class_callback' => function( $classes ) { $classes[] = get_query_var( 'review_category' ); return $classes; }, 'title_callback' => function( $title, $seperator ) { return review_category . ' ' . $seperator . ' ' . $title; }, 'rewrite_tests_callback' => function() { return array( 'Review Category' => array( '/reviews/foo/', '/reviews/bar/', ), ); } ) );
Contribution guidelines
see https://github.com/humanmade/hm-rewrite/blob/master/CONTRIBUTING.md
humanmade/hm-rewrite 适用场景与选型建议
humanmade/hm-rewrite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.57k 次下载、GitHub Stars 达 160, 最近一次更新时间为 2022 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 humanmade/hm-rewrite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 humanmade/hm-rewrite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 16.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 160
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2022-09-21