CakePHP 3.0 .json 缺少模板错误
CakePHP 3.0 .json missing template error
我正在将现有的 2.5 应用程序迁移到 3.0。现在使用 json 请求时出现缺少模板的错误,而我在旧版本中没有。
我没有看到我可能错过的任何步骤。
routes.php
Router::extensions(['json', 'xml']);
PagesController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
public function request(){
$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
}
之前的应用能够检测到请求类型并且 return 具有相同的类型。无需设置渲染。
我发现因为我正在尝试访问 PagesController.php 上的操作,所以 routes.php 试图通过查找模板的显示操作传递它。
routes.php
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
PagesController.php
public function display()
{
$path = func_get_args();
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
$this->set(compact('page', 'subpage'));
try {
$this->render(implode('/', $path));
} catch (MissingTemplateException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
我正在将现有的 2.5 应用程序迁移到 3.0。现在使用 json 请求时出现缺少模板的错误,而我在旧版本中没有。
我没有看到我可能错过的任何步骤。
routes.php
Router::extensions(['json', 'xml']);
PagesController.php
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
public function request(){
$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
}
之前的应用能够检测到请求类型并且 return 具有相同的类型。无需设置渲染。
我发现因为我正在尝试访问 PagesController.php 上的操作,所以 routes.php 试图通过查找模板的显示操作传递它。
routes.php
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
PagesController.php
public function display()
{
$path = func_get_args();
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
$this->set(compact('page', 'subpage'));
try {
$this->render(implode('/', $path));
} catch (MissingTemplateException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}