Wordpress Timber 插件自定义路由抛出 404

问题描述

木材路线在为我的自定义路线做卷曲时抛出 404,如下所示。 GoogleBots 也将其视为 404 页。

λ curl -I http://mysite.test/this-is-a-test/

结果:

HTTP/1.1 404 Not Found
Date: Fri,23 Apr 2021 03:44:00 GMT
Server: Apache/2.4.39 (Win64) OpenSSL/1.1.1b PHP/7.3.8
Strict-Transport-Security: max-age=31536000
Expires: Wed,11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache,must-revalidate,max-age=0
Link: <http://mysite.test/wp-json/>; rel="https://api.w.org/"
Content-Type: text/html; charset=UTF-8

虽然它在浏览器中运行良好。

我该如何解决这个问题?任何帮助将不胜感激。谢谢

代码示例

Routes::map('this-is-a-test/',function ($params) {
    http_response_code(200);
    echo 'Done'; // I also tried Routes::load('test.PHP',$params); both shows same result
    exit;
});

解决方法

你需要添加一个load方法让路由生效:

Routes::map('this-is-a-test/',function ($params) {
    Routes::load( $php_file,$args,$query = null,$status_code = 200 )
});

Timber 使用一个名为 Upstatement Routes 的库,其中 AltoRouter 需要 php 文件。