Cakephp3:无法识别/配置调用的扩展名:html

时间:2018-12-25 11:29:23

标签: cakephp routes cakephp-3.0

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

1 个答案:

答案 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;
        }
    });

    // ...
}