大脑/皮层

皮层是一个实现WordPress路由系统的包。

1.0.0-alpha.11 2023-09-18 08:17 UTC

README

Travis CI codecov.io MIT license

皮层是基于FastRoute的WordPress路由系统

开始使用皮层

首先确保Composer自动加载已启用。

然后“启动”皮层

Brain\Cortex::boot();

这可以尽早完成,无需包装在钩子中。

'do_parse_request'钩子被触发后,它将不会工作。

添加路由

要添加路由,可以使用'cortex.routes'钩子,它传递一个RouteCollectionInterface实例

use Brain\Cortex\Route\RouteCollectionInterface;
use Brain\Cortex\Route\QueryRoute;

add_action('cortex.routes', function(RouteCollectionInterface $routes) {
	
	$routes->addRoute(new QueryRoute(
		'{type:[a-z]+}/latest',
		function(array $matches) {
		  return [
		    'post_type'      => $matches['type'],
		    'posts_per_page' => 5,
		    'orderby'        => 'date',
		    'order'          => 'ASC'
		  ];
		}
	));
});

路由模式(第一个参数)语法继承自FastRoute。

作为第二个参数传递的回调函数接收匹配数组(FastRoute中的$routeInfo[2])并必须返回一个用于WP_Query的参数数组。

QueryRoute参数

QueryRoute构造函数接受第三个参数,即路由配置的选项数组。

其中之一是强制WordPress在路由匹配时使用模板的"template"

add_action('cortex.routes', function(RouteCollectionInterface $routes) {
	
	$routes->addRoute(new QueryRoute(
		'post/latest',
		function(array $matches) {
		  return [
		    'orderby'        => 'date',
		    'order'          => 'DESC'
		  ];
		},
		['template' => 'latest.php']
	));
});

如上所示,template参数可以是主题(或子主题)文件夹的相对路径。

要使用位于主题文件夹之外的主题,template参数需要是模板文件的完整绝对路径。

还有其他参数,其中

  • "before"和"after",分别在调用返回查询参数的回调之前和之后运行
  • "host",使路由仅针对特定主机匹配
  • "method",使路由仅针对特定HTTP方法(例如POSTGET)匹配
  • "scheme",使路由仅针对特定HTTP方案(例如httpshttp)匹配
  • "group",从一个或多个“路由组”使用配置
  • "priority",强制按特定顺序评估路由(优先级较低者先评估)
  • "merge_query_string",允许(默认)或避免将URL查询字符串合并为路由回调返回的内容的查询参数

路由组

路由组是一种在路由之间共享常用设置的方法。

在将组分配给路由之前,我们需要添加组。

可以使用'cortex.groups'钩子完成此操作,该钩子传递一个GroupCollectionInterface实例

use Brain\Cortex\Route\RouteCollectionInterface;
use Brain\Cortex\Group\GroupCollectionInterface;
use Brain\Cortex\Route\QueryRoute;
use Brain\Cortex\Group\Group;

add_action('cortex.groups', function(GroupCollectionInterface $groups) {
	
	$groups->addGroup(new Group([
	    'id'       => 'archive-group',
	    'template' => 'archive.php',
	    'before'   => function() {
	       // do something before route callback
	    }
	]));
});

add_action('cortex.routes', function(RouteCollectionInterface $routes) {
	
	$routes->addRoute(new QueryRoute(
	    '^post/latest$',
	    function(array $matches) {
	        return [
	            'orderby'        => 'date',
	            'order'          => 'DESC'
	        ];
	    },
	    ['group' => 'archive-group']
	));
	
	$routes->addRoute(new QueryRoute(
	    'post/oldest',
	    function(array $matches) {
	        return [
	            'orderby'        => 'date',
	            'order'          => 'ASC'
	         ];
	     },
	     ['group' => 'archive-group']
	));
});

通过将其构造函数传递一个值数组来实例化一个组。值"ID"是必需的。所有其他值都是可选的,可以用来设置任何路由属性(QueryRoute构造函数的第三个参数中的数组项)。

要在路由中使用组属性,必须在'group'路由属性中设置组ID。

'group'属性还接受一个组ID数组,以分配来自多个组的属性。

重定向路由

QueryRoute是Cortex附带的路由之一。还有其他路由,您还可以编写自定义路由,实现Brain\Cortex\Route\RouteInterface

另一个在Cortex中包含的实现是RedirectRoute。正如其名称所示,它用于将URL重定向到其他URL。

use Brain\Cortex\Route\RouteCollectionInterface;
use Brain\Cortex\Route\RedirectRoute;

add_action('cortex.routes', function(RouteCollectionInterface $routes) {
	
	$routes->addRoute(new RedirectRoute(
		'old/url/{postname}',
		function(array $matches) {
		  return 'new/url/' . $matches['postname'];
		}
	));
});

RedirectRoute接受一个选项数组。

可以使用选项来配置要使用的HTTP状态码('redirect_status'选项,默认为302)以及是否允许将重定向到外部URL('redirect_external'选项,默认为false)。

安装

通过Composer,要求版本为~1.0.0brain/cortex

composer require brain/cortex:~1.0.0

您可能需要降低项目最低稳定性要求。

composer config minimum-stability dev

最低要求

  • PHP 5.5+
  • Composer 安装

依赖

  • 任何版本的 PSR7 接口(无需实现)
  • FastRoute

许可证

MIT