cakePHP 3.0操作PagesController :: myaction.json()找不到,或无法访问. [英] cakePHP 3.0 Action PagesController::myaction.json() could not be found, or is not accessible."

查看:74
本文介绍了cakePHP 3.0操作PagesController :: myaction.json()找不到,或无法访问.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将现有的2.5应用程序迁移到3.0.使用ajax时出现404错误.在cakePHP 2.5中可以正常工作

I am migrating an existing 2.5 app over to 3.0. I am getting an 404 error when using ajax. This works fine in cakePHP 2.5

url:"/cakephp3/pages/myaction.json"

url: "/cakephp3/pages/myaction.json"

我看不到我可能错过的任何步骤.

I don't see any step that I might have missed.

我确定这是扩展名为.json的路由问题

I am sure it is a routing issue with the .json extension

routes.php

routes.php

Router::scope('/', function ($routes) {

    Router::extensions(['json', 'xml']);

    $routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);
    $routes->connect('/hotel-training-courses', ['controller' => 'pages', 'action' => 'trainingCourses']);
    $routes->connect('/feature-tour', ['controller' => 'pages', 'action' => 'features']);
    $routes->connect('/contact-us', ['controller' => 'pages', 'action' => 'contact']);
    $routes->fallbacks('InflectedRoute');
});

PagesController.php

PagesController.php

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
}

public function myaction(){

    $this->request->onlyAllow('ajax');

    $userName = $this->request->data['name'];
    $userCompany = $this->request->data['company'];
    $userEmail = $this->request->data['email'];
    $userPhone = $this->request->data['phone'];

    //send an email

}

先前的应用程序能够检测到请求类型并以相同的类型返回.无需设置渲染.

The previous app was able to detect the request type and return with the same type. There was no need to set the render.

推荐答案

全局扩展必须在范围之外定义

Router :: extensions()必须放置在外部,以防应应用于所有路由,请在定义任何作用域和路由之前调用.

Global extensions must be defined outside of scopes

Router::extensions() must be placed outside of, and in case it should apply to all routes, invoked before defining any scopes and routes.

如果要将扩展名解析限制在特定范围内,请使用 RouteBuilder :: extensions(),即任一

In case you want to restrict extension parsing to a specific scope, use RouteBuilder::extensions(), ie either

Router::extensions(['json', 'xml']);

Router::scope('/', function (RouteBuilder $routes) {
    $routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);
    //...
});

Router::scope('/', function (RouteBuilder $routes) {
    $routes->extensions(['json', 'xml']);

    $routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);
    //...
});

请参见 Cookbook>路由>路由文件扩展程序

See Cookbook > Routing > Routing File Extensions

Request :: onlyAllow()已重命名为 Request :: allowMethod(),所以这是您遇到的下一个问题.

Request::onlyAllow() has been renamed to Request::allowMethod(), so that's the next problem that you'll encouter.

请参见

还应该启用调试模式,以便收到有意义的错误消息,其中包含调试此类问题所必需的适当详细信息.

Also you should enable debug mode so that you receive meaningful error messages with the appropriate details, necessary to debug such problems.

这篇关于cakePHP 3.0操作PagesController :: myaction.json()找不到,或无法访问.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆