无法从元素访问控制器
Can't access controller from element
我正在尝试使用从元素内部的控制器填充的内容,以便我可以使用该内容构建视图。
到目前为止我有以下元素(.ctp 文件)
<?php $categories = $this->requestAction('categories/menu'); ?>
<ul class="hidden">
<?php foreach ($categories as $categorie): ?>
<li><?= $this->Html->link($categorie->name, '#'); ?></li>
<?php endforeach; ?>
</ul>
用这个控制器
class CategoriesController extends AppController
{
public function index()
{
$categories = $this->Categories->find('all');
$this->set(compact('categories'));
}
public function menu()
{
$categories = $this->paginate();
$this->set('categories', $categories);
}
}
使用此处的代码时,日志中出现错误,页面上没有任何显示 Error: [Cake\View\Exception\MissingTemplateException] Template file "Categories\menu.ctp" is missing.
。
如果我为这个 $this->set('categories', $categories);
更改此行 return $categories;
我的页面上会显示以下错误 Error: Controller action can only return an instance of Response
。
我最初尝试了 this tutorial 中的代码(抱歉滚动到 缓存元素 之前的最后一个示例)但它导致了第二条错误消息。
如何使用 Element
中的 Controller
?
这是预期的行为,控制器需要一个视图模板来呈现,除非您通过将 Controller::$autoRender
设置为 false
,通过使方法 return 一个 Response
对象,或者使用 serialized data views.
后者是错误信息告诉你的方法,预计return,见http://book.cakephp.org/3.0/en/controllers.html#controller-actions
When you use controller methods with Routing\RequestActionTrait::requestAction()
you will typcially return a Response
instance. If you have controller methods that are used for normal web requests + requestAction, you should check the request type before returning
// src/Controller/RecipesController.php
class RecipesController extends AppController
{
public function popular()
{
$popular = $this->Recipes->find('popular');
if (!$this->request->is('requested')) {
$this->response->body(json_encode($popular));
return $this->response;
}
$this->set('popular', $popular);
}
}
[...]
查看单元格
您似乎只想获取和渲染模型数据,我建议您查看视图单元格。
你应该具备的是:
public function menu()
{
$categories = $this->Categories->find('all');
if($this->request->is('requested'):
$this->response->body($categories);
endif;
}
我正在尝试使用从元素内部的控制器填充的内容,以便我可以使用该内容构建视图。
到目前为止我有以下元素(.ctp 文件)
<?php $categories = $this->requestAction('categories/menu'); ?>
<ul class="hidden">
<?php foreach ($categories as $categorie): ?>
<li><?= $this->Html->link($categorie->name, '#'); ?></li>
<?php endforeach; ?>
</ul>
用这个控制器
class CategoriesController extends AppController
{
public function index()
{
$categories = $this->Categories->find('all');
$this->set(compact('categories'));
}
public function menu()
{
$categories = $this->paginate();
$this->set('categories', $categories);
}
}
使用此处的代码时,日志中出现错误,页面上没有任何显示 Error: [Cake\View\Exception\MissingTemplateException] Template file "Categories\menu.ctp" is missing.
。
如果我为这个 $this->set('categories', $categories);
更改此行 return $categories;
我的页面上会显示以下错误 Error: Controller action can only return an instance of Response
。
我最初尝试了 this tutorial 中的代码(抱歉滚动到 缓存元素 之前的最后一个示例)但它导致了第二条错误消息。
如何使用 Element
中的 Controller
?
这是预期的行为,控制器需要一个视图模板来呈现,除非您通过将 Controller::$autoRender
设置为 false
,通过使方法 return 一个 Response
对象,或者使用 serialized data views.
后者是错误信息告诉你的方法,预计return,见http://book.cakephp.org/3.0/en/controllers.html#controller-actions
When you use controller methods with
Routing\RequestActionTrait::requestAction()
you will typcially return aResponse
instance. If you have controller methods that are used for normal web requests + requestAction, you should check the request type before returning// src/Controller/RecipesController.php class RecipesController extends AppController { public function popular() { $popular = $this->Recipes->find('popular'); if (!$this->request->is('requested')) { $this->response->body(json_encode($popular)); return $this->response; } $this->set('popular', $popular); } }
[...]
查看单元格
您似乎只想获取和渲染模型数据,我建议您查看视图单元格。
你应该具备的是:
public function menu()
{
$categories = $this->Categories->find('all');
if($this->request->is('requested'):
$this->response->body($categories);
endif;
}