Cakephp3:调用的扩展不是 recognized/configured:html

Cakephp3 : Invoked extension not recognized/configured: html

我在 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);
});

这可能会在 3.7.2 中得到修复,请参阅 https://github.com/cakephp/cakephp/pull/12845

在那之前,一种解决方法是在 Controller.startup 事件中取消请求处理程序组件上的扩展,例如在您的 AppController class' 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;
        }
    });

    // ...
}