cowshed/router

0.0.5-alpha 2021-03-14 13:54 UTC

README

此包允许您注册硬编码的 WordPress 页面。

在底层,它创建基于 CMS 的页面,这些页面存储在数据库中。

此包对于具有预定义内容的插件或主题非常有用,例如在特定路由上具有自己模板的仪表板。

使用方法

为防止任何意外行为,请在 init 钩子内部使用此包的功能。

注册路由

add_action( 'init', function () {
    $router = new Router();
    $router->route( [
        'name'     => 'home', // slug my-custom-route
        'template' => 'views/template-home.blade.php', // template path
        'ref'      => 'home', // reference for URL fetching
    ] )->route( [
        'name'     => 'about us',
        'template' => 'views/template-about.blade.php',
        'ref'      => 'about',
    ] )->bind();
} );

获取路由 URL

\Cowshed\Router\Router::url('test')

Router 类中的大多数方法都是链式的。

方法

route

将路由添加到列表中。

参数

  • route - 数组 - 包含 nametemplate & ref 键。

返回

  • Router 实例

bind

为路由创建页面。

返回

  • Router 实例

url - 静态

获取指定路由的 URL。

参数

  • ref - 字符串 - 在 route 调用中指定的路由引用。

返回

  • mixed string|False- 如果找到,则为 URL,如果没有找到,则为 false。

错误

返回一个包含错误和通知的数组。

返回

  • 数组 - 错误和通知的列表。