我在Cakephp 3.7.1中有一个令人沮丧的错误。我知道类似的错误已在 https://discourse.cakephp.org/t/invoked-extension-not-recognized-configured-html/5355。
错误是xml
扩展名有效,而html
扩展名无效。
这是我的路线文件。
<?php
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
$routesArray = [
'/' => [
'defaults' => [
'controller' => 'Pages',
'action' => 'view',
'home',
],
'options' => [],
],
'/pages/*' => [
'defaults' => [
'controller' => 'Pages',
'action' => 'display',
],
'options' => [],
],
'/pages/:page' => [
'defaults' => [
'controller' => 'Pages',
'action' => 'view',
],
'options' => [
'pass' => [
'page',
],
],
],
];
$languages = ['sn', 'fr', 'en'];
foreach ($languages as $language) {
Router::prefix($language, function ($routes) use ($routesArray, $language) {
$routes->setExtensions([
'xml',
'html',
]);
foreach ($routesArray as $key => $value) {
if (!empty($value['options']['_name'])) {
$value['options']['_name'] .= $language;
}
$routes->connect(
$key,
$value['defaults'],
$value['options']
);
}
$routes->fallbacks(DashedRoute::class);
});
}
Router::scope('/', function ($routes) use ($routesArray) {
$routes->setExtensions([
'xml',
'html',
]);
foreach ($routesArray as $key => $value) {
$routes->connect(
$key,
$value['defaults'],
$value['options']
);
}
$routes->fallbacks(DashedRoute::class);
});
答案 0 :(得分:1)
此问题可能会在3.7.2中修复,请参见 https://github.com/cakephp/cakephp/pull/12845 。
在此之前,一种解决方法是在Controller.startup
事件中取消请求处理程序组件上的扩展名,例如在AppController
类的beforeFilter()
方法中进行如下设置:>
public function beforeFilter(\Cake\Event\Event $event)
{
parent::beforeFilter($event);
$this->getEventManager()->on('Controller.startup', function () {
if ($this->RequestHandler->ext === 'html') {
$this->RequestHandler->ext = null;
}
});
// ...
}