Cakephp .json ext 给出缺少的方法
Cakephp .json ext giving Missing Method
我有一个下拉菜单 onChange
我使用 ajax 从控制器中的方法加载一些 .json。
但是我得到 error 404
returned
如果我删除 .json 扩展名,我会得到 error 500
缺少的模板,我也无法解决。我尝试了不同的解决方案。我宁愿使用 .json 分机,让 cakephp return 格式正确 JSON.
Missing Method in StrategiesConditionsController
Cake\Controller\Exception\MissingActionException
Error The action conditions.json is not defined in StrategiesConditionsController
Create StrategiesConditionsController::conditions.json() in file: src/Controller/StrategiesConditionsController.php.
通过多次阅读文档,我确信我的路线是正确的。
routes.php
<?php
/**
* Routes configuration.
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different URLs to chosen controllers and their actions (functions).
*
* It's loaded within the context of `Application::routes()` method which
* receives a `RouteBuilder` instance `$routes` as method argument.
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
/*
* The default class to use for all routes
*
* The following route classes are supplied with CakePHP and are appropriate
* to set as the default:
*
* - Route
* - InflectedRoute
* - DashedRoute
*
* If no call is made to `Router::defaultRouteClass()`, the class used is
* `Route` (`Cake\Routing\Route\Route`)
*
* Note that `Route` does not do any inflections on URLs which will result in
* inconsistently cased URLs when used with `:plugin`, `:controller` and
* `:action` markers.
*/
/** @var \Cake\Routing\RouteBuilder $routes */
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $routes) {
$routes->setExtensions(['json']);
$routes->resources('StrategiesConditions', [
'map' => [
'conditions' => [
'action' => 'conditions',
'method' => 'post'
]
],
'only' => ['conditions']
]);
});
$routes->scope('/', function (RouteBuilder $builder) {
// Register scoped middleware for in scopes.
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
'httpOnly' => true,
]));
/*
* Apply a middleware to the current route scope.
* Requires middleware to be registered through `Application::routes()` with `registerMiddleware()`
*/
$builder->applyMiddleware('csrf');
/*
* Here, we are connecting '/' (base path) to a controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, templates/Pages/home.php)...
*/
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
/*
* ...and connect the rest of 'Pages' controller's URLs.
*/
$builder->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
/*
* Connect catchall routes for all controllers.
*
* The `fallbacks` method is a shortcut for
*
* ```
* $builder->connect('/:controller', ['action' => 'index']);
* $builder->connect('/:controller/:action/*', []);
* ```
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$builder->fallbacks();
});
Router::prefix('admin', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->fallbacks(DashedRoute::class);
});
/*
* If you need a different set of middleware or none at all,
* open new scope and define routes there.
*
* ```
* $routes->scope('/api', function (RouteBuilder $builder) {
* // No $builder->applyMiddleware() here.
* // Connect API actions here.
* });
* ```
*/
策略条件控制器
public function conditions()
{
$this->request->allowMethod(['post']);
if ($this->request->is('ajax')) {
$this->response = $this->response->withDisabledCache();
}
$strategy_id = $this->request->getData('strategy_id');
$strategiesConditions = $this->StrategiesConditions->find('all', [
'where' => ['strategy_id' => $strategy_id],
]);
$this->viewBuilder()->setOption('serialize', ['strategiesConditions']);
}
Cakephp控制台有8条路由显示
Route name URI template Defaults
pages:display /
{
"controller": "Pages",
"action": "display",
"0": "home",
"plugin": null
}
pages:display /pages/*
{
"controller": "Pages",
"action": "display",
"plugin": null
}
_controller:index /{controller}
{
"action": "index",
"plugin": null
}
_controller:_action /{controller}/{action}/*
{
"plugin": null,
"action": "index"
}
admin:users:index /admin
{
"controller": "Users",
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:index /admin/{controller}
{
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:_action /admin/{controller}/{action}/*
{
"prefix": "Admin",
"plugin": null,
"action": "index"
}
strategiesconditions:conditions /strategies-conditions/conditions
{
"controller": "StrategiesConditions",
"action": "conditions",
"_method": "post",
"plugin": null
}
从调试工具包中的历史选项卡更新
Request
Routing Params
controller StrategiesConditions
action conditions.json
pass (empty)
plugin (null)
_matchedRoute /{controller}/{action}/*
_ext (null)
Post data
strategy_id 11
Query string
No querystring data.
Cookie
csrfToken dd12f852560a384d39206e511f1857f77f71da2eadb023a6c67ae346
PHPSESSID 3d2jt7dgpo09af8b1908quljpn
Matched Route
template /{controller}/{action}/*
和路线选项卡:
Routes
Toggle debugkit internal routes
Route name URI template Defaults
pages:display /
{
"controller": "Pages",
"action": "display",
"0": "home",
"plugin": null
}
pages:display /pages/*
{
"controller": "Pages",
"action": "display",
"plugin": null
}
_controller:index /{controller}
{
"action": "index",
"plugin": null
}
_controller:_action /{controller}/{action}/*
{
"plugin": null,
"action": "index"
}
admin:users:index /admin
{
"controller": "Users",
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:index /admin/{controller}
{
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:_action /admin/{controller}/{action}/*
{
"prefix": "Admin",
"plugin": null,
"action": "index"
}
strategiesconditions:conditions /strategies-conditions/conditions
{
"controller": "StrategiesConditions",
"action": "conditions",
"_method": "post",
"plugin": null
}
请求URL
http://localhost:8888/trading-journal/strategies-conditions/conditions.json
Application.php
// Add routing middleware.
// If you have a large number of routes connected, turning on routes
// caching in production could improve performance. For that when
// creating the middleware instance specify the cache config name by
// using it's second constructor argument:
// `new RoutingMiddleware($this, '_cake_routes_')`
->add(new RoutingMiddleware($this))
'method' => 'POST'
应该是大写的。这不是文件。已提出更改请求,因此不区分大小写。
$routes->scope('/', function (RouteBuilder $routes) {
$routes->setExtensions(['json']);
$routes->resources('StrategiesConditions', [
'map' => [
'conditions' => [
'action' => 'conditions',
'method' => 'POST'
]
],
'only' => ['conditions']
]);
});
我有一个下拉菜单 onChange
我使用 ajax 从控制器中的方法加载一些 .json。
但是我得到 error 404
returned
如果我删除 .json 扩展名,我会得到 error 500
缺少的模板,我也无法解决。我尝试了不同的解决方案。我宁愿使用 .json 分机,让 cakephp return 格式正确 JSON.
Missing Method in StrategiesConditionsController
Cake\Controller\Exception\MissingActionException
Error The action conditions.json is not defined in StrategiesConditionsController
Create StrategiesConditionsController::conditions.json() in file: src/Controller/StrategiesConditionsController.php.
通过多次阅读文档,我确信我的路线是正确的。
routes.php
<?php
/**
* Routes configuration.
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different URLs to chosen controllers and their actions (functions).
*
* It's loaded within the context of `Application::routes()` method which
* receives a `RouteBuilder` instance `$routes` as method argument.
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
/*
* The default class to use for all routes
*
* The following route classes are supplied with CakePHP and are appropriate
* to set as the default:
*
* - Route
* - InflectedRoute
* - DashedRoute
*
* If no call is made to `Router::defaultRouteClass()`, the class used is
* `Route` (`Cake\Routing\Route\Route`)
*
* Note that `Route` does not do any inflections on URLs which will result in
* inconsistently cased URLs when used with `:plugin`, `:controller` and
* `:action` markers.
*/
/** @var \Cake\Routing\RouteBuilder $routes */
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $routes) {
$routes->setExtensions(['json']);
$routes->resources('StrategiesConditions', [
'map' => [
'conditions' => [
'action' => 'conditions',
'method' => 'post'
]
],
'only' => ['conditions']
]);
});
$routes->scope('/', function (RouteBuilder $builder) {
// Register scoped middleware for in scopes.
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
'httpOnly' => true,
]));
/*
* Apply a middleware to the current route scope.
* Requires middleware to be registered through `Application::routes()` with `registerMiddleware()`
*/
$builder->applyMiddleware('csrf');
/*
* Here, we are connecting '/' (base path) to a controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, templates/Pages/home.php)...
*/
$builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
/*
* ...and connect the rest of 'Pages' controller's URLs.
*/
$builder->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
/*
* Connect catchall routes for all controllers.
*
* The `fallbacks` method is a shortcut for
*
* ```
* $builder->connect('/:controller', ['action' => 'index']);
* $builder->connect('/:controller/:action/*', []);
* ```
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$builder->fallbacks();
});
Router::prefix('admin', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->fallbacks(DashedRoute::class);
});
/*
* If you need a different set of middleware or none at all,
* open new scope and define routes there.
*
* ```
* $routes->scope('/api', function (RouteBuilder $builder) {
* // No $builder->applyMiddleware() here.
* // Connect API actions here.
* });
* ```
*/
策略条件控制器
public function conditions()
{
$this->request->allowMethod(['post']);
if ($this->request->is('ajax')) {
$this->response = $this->response->withDisabledCache();
}
$strategy_id = $this->request->getData('strategy_id');
$strategiesConditions = $this->StrategiesConditions->find('all', [
'where' => ['strategy_id' => $strategy_id],
]);
$this->viewBuilder()->setOption('serialize', ['strategiesConditions']);
}
Cakephp控制台有8条路由显示
Route name URI template Defaults
pages:display /
{
"controller": "Pages",
"action": "display",
"0": "home",
"plugin": null
}
pages:display /pages/*
{
"controller": "Pages",
"action": "display",
"plugin": null
}
_controller:index /{controller}
{
"action": "index",
"plugin": null
}
_controller:_action /{controller}/{action}/*
{
"plugin": null,
"action": "index"
}
admin:users:index /admin
{
"controller": "Users",
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:index /admin/{controller}
{
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:_action /admin/{controller}/{action}/*
{
"prefix": "Admin",
"plugin": null,
"action": "index"
}
strategiesconditions:conditions /strategies-conditions/conditions
{
"controller": "StrategiesConditions",
"action": "conditions",
"_method": "post",
"plugin": null
}
从调试工具包中的历史选项卡更新
Request
Routing Params
controller StrategiesConditions
action conditions.json
pass (empty)
plugin (null)
_matchedRoute /{controller}/{action}/*
_ext (null)
Post data
strategy_id 11
Query string
No querystring data.
Cookie
csrfToken dd12f852560a384d39206e511f1857f77f71da2eadb023a6c67ae346
PHPSESSID 3d2jt7dgpo09af8b1908quljpn
Matched Route
template /{controller}/{action}/*
和路线选项卡:
Routes
Toggle debugkit internal routes
Route name URI template Defaults
pages:display /
{
"controller": "Pages",
"action": "display",
"0": "home",
"plugin": null
}
pages:display /pages/*
{
"controller": "Pages",
"action": "display",
"plugin": null
}
_controller:index /{controller}
{
"action": "index",
"plugin": null
}
_controller:_action /{controller}/{action}/*
{
"plugin": null,
"action": "index"
}
admin:users:index /admin
{
"controller": "Users",
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:index /admin/{controller}
{
"action": "index",
"prefix": "Admin",
"plugin": null
}
admin:_controller:_action /admin/{controller}/{action}/*
{
"prefix": "Admin",
"plugin": null,
"action": "index"
}
strategiesconditions:conditions /strategies-conditions/conditions
{
"controller": "StrategiesConditions",
"action": "conditions",
"_method": "post",
"plugin": null
}
请求URL
http://localhost:8888/trading-journal/strategies-conditions/conditions.json
Application.php
// Add routing middleware.
// If you have a large number of routes connected, turning on routes
// caching in production could improve performance. For that when
// creating the middleware instance specify the cache config name by
// using it's second constructor argument:
// `new RoutingMiddleware($this, '_cake_routes_')`
->add(new RoutingMiddleware($this))
'method' => 'POST'
应该是大写的。这不是文件。已提出更改请求,因此不区分大小写。
$routes->scope('/', function (RouteBuilder $routes) {
$routes->setExtensions(['json']);
$routes->resources('StrategiesConditions', [
'map' => [
'conditions' => [
'action' => 'conditions',
'method' => 'POST'
]
],
'only' => ['conditions']
]);
});