lucatume / wp-routing
WordPress 中模仿 Laravel 路由尝试
Requires
- php: >=5.2.0
- lucatume/tdd-helpers: 2.*
- lucatume/wp-utils: 2.*
- lucatume/wp-wrappers: 2.*
Requires (Dev)
- codeception/codeception: 1.8.*@dev
- codeception/remote-debug: dev-master
README
已弃用:此包不再维护。有关可能的替代方案,请参阅 wp-routes。
一组包装类,通过 WP Router 插件和来自 Laravel 的某些包,应在 WordPress 中实现类似 Laravel 的路由。
将库包含在主题或插件中
该库旨在与 Composer 一起在 Composer 管理的项目中使用,要包含在项目中,应在 composer.json
的 require 条目中添加以下行
{
"require": {
"lucatume/wp-routing": "dev-master"
}
}
WPRouting_Route
设置路由
虽然包装类的内部工作将基于 WP Router 代码,因此任何示例都将在 Laravel 类似的接口中包装设置路由所需的方法,从以下内容转换为以下内容
// file my-routes-plugin.php
add_action('wp_router_generate_routes', 'generateMyRoutes');
function generateMyRoutes(WP_Router $router)
{
$router->add_route('wp-router-sample', array(
'path' => '^wp_router/(.*?)$',
'query_vars' => array(
'sample_argument' => 1,
),
'page_callback' => array(get_class(), 'sample_callback'),
'page_arguments' => array('sample_argument'),
'access_callback' => TRUE,
'title' => 'WP Router Sample Page',
'template' => array('sample-page.php', dirname(__FILE__).DIRECTORY_SEPARATOR.'sample-page.php')
));
}
到以下内容
// file my-routes-plugin.php
WPRouting_Route::get('wp_router/{word}', function($word){
echo "Hello $word";
})->where('word', '.*?')
->withTitle('Wp Router Sample Page')
->withTemplate(array(
'sample-page',
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sample-page.php'
);
路由方法
任何路由生成的入口点是以下 4 个方法之一
WPRouting_Route::get
处理GET
请求WPRouting_Route::post
处理POST
请求WPRouting_Route::put
处理PUT
请求WPRouting_Route::delete
处理DELETE
请求WPRouting_Route::all
处理所有请求
上述每个方法都将接受两个参数
- 相对于根 URL 的路径
- 过滤器、回调参数参数
路径可以包含示例中像 {word}
这样的占位符字符串,这将需要稍后在流畅链中使用 where
方法进行定义。
第二个参数可以是简单的回调函数,也可以是包含以下内容的数组或管道字符分隔的列表
- 一个数组或管道字符分隔的列表
- 一个回调函数
任何过滤器都需要使用 filter
静态方法进行注册。
withTitle
:设置路由页面标题,可以是字符串或回调函数。withTemplate
:设置用于显示路由的模板,遵循 WP Router 的相同机制。with
:设置要附加到路由的元信息的键/值对。withId
:显式设置路由 ID,通常路由 ID 会通过使用短划线连接路径来构造。where
:设置用于路由路径的模式。filter
:静态,设置用于路由访问回调的过滤器。pattern
:静态,设置用于所有路由的模式。
示例
请注意,路径变量将按其出现顺序传递给页面、访问和标题回调方法/函数。
我想添加一个显示文章存档的 /posts
页面
WPRouting_Route::get('posts', function(){
echo 'My post archive';
})->withTitle('Archive');
我想添加一个只有管理员可以访问的 /secret-posts
页面
WPRouting_Route::filter('admin', function(){
return current_user_can('activate_plugins');
}) ;
WPRouting_Route::get('secret-posts', array('admin', function(){
echo 'Secret posts';
}))->withTitle('Secret page');
我想为编辑器添加编辑文章的 PUT 和 POST 端点
WPRouting_Route::filter('editor', function($id){
return current_user_can('edit_posts', $id);
});
WPRouting_Route::pattern('id', '\d+');
WPRouting_Route::put('posts/{id}', array('editor', function($id){
echo 'Doing some post updating';
}));
WPRouting_Route::post('posts/{id}', array('editor', function($id){
echo 'Adding a post';
}));
WPRouting_PersistableRoute
WPRouting_Route
基类的一个扩展,允许将路由元信息持久化到 WordPress 数据库。
持久化路由元信息
该类定义了一个接受布尔值的 shouldBePersisted
方法,当设置为 true
时将触发路由元信息;默认情况下,路由将不会被持久化。
至少,一个路由必须定义一个标题和一个路径才有资格进行持久化;它是一个流畅的方法,可以像以下这样使用
// file my-routes-plugin.php
WPRouting_PersistableRoute::get('hello', function(){
echo "Hello there";
})->withTitle('Wp Router Sample Page')
->withTemplate(array(
'sample-page',
dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sample-page.php'
)
->shouldBePersisted();
shouldBePersisted
方法在未传入任何值时默认为 true
,因此
->shouldBePersisted();
->shouldBePersisted(true);
如果将路由元数据的持久化设置为 true
,则任何与 WP Router 无关的路由参数(请参阅 WPRouting_Route::$WPRouterArgs
变量)以及未被排除在持久化之外的参数(请参阅 WPRouting_PersistableRoute::$nonPersistingArgs
变量)将被持久化为路由元信息。所有路由元信息将存储在 WordPress 数据库中的一个选项中(请参阅 WPRouting_PersistableRoute::OPTION_ID
常量以获取其值),并以以下结构存储在数组中:
[
'route-one' :
['title' => 'Route One', 'permalink' => 'route-one', 'some-meta' => 'some meta value']
'route-two' :
['title' => 'Route Two', 'permalink' => 'route-two', 'some-meta' => 'some meta value']
]
其中第一级数组键是路由 ID。
挂钩以修改路由元信息
在将路由元信息持久化到数据库之前以及 在进行参数修剪之后,WPRouting_PersistableRoute
类提供了一个过滤器钩子(请参阅 WPRouting_PersistableRoute::ROUTE_PERSISTED_VALUES_FILTER
以获取其标签)来修改在数据库中持久化之前的路由元信息。
过滤器函数的参数是路由参数和路由 ID,过滤器将为每个路由触发一次。