我该如何在CakePHP 3.x的视图中使用requestAction [英] How should I use requestAction in the view with CakePHP 3.x
问题描述
我的代码:
// View/Activities/index.ctp
...
<div>
<?php echo $this->requestAction('/Activities/ajax_list/'.$categoryId,['return']);?>
</div>
...
//View/Activitest/ajax_list.ctp
....
<?php echo $this -> Html -> image("/img/add1.jpg"); ?>
...
<?php echo $this->Html->link('add_project', array('controller'=>'projects', 'action'=>'add', $categoryId)); ?>
....
我想将视图 ajax_list包含到索引中',并且它已经显示,但是图像和链接的网址不正确。
I want to include the view 'ajax_list' into the 'index',and it has been displayed but the url of image and link was wrong.
然后我调试了Cake / Routing / RequestActionTrait.php,找到了 requestAction功能 $ request =新请求($ params); $ request-> base,$ request-> webroot为空。
Then I debug the Cake/Routing/RequestActionTrait.php , "requestAction" function I find "$request = new Request($params);" the $request->base , $request->webroot are null.
有人可以告诉我如何解决吗?
Could some one tell me how should I fix it?
推荐答案
$ base
/ $ webroot
属性不在新请求中设置可能被认为是错误,或者文档只是缺少适当的示例解决方案,我不能肯定地说,您可能想 在GitHub上报告此问题 ,看看开发人员怎么说。
The $base
/$webroot
properties not being set in the new request might be considered a bug, or maybe the docs are just missing a proper example solution, I can't tell for sure, you might want to report this over at GitHub and see what the devs say.
只要适用,最好使用查看单元格而不是请求操作,因为它们避免了调度新请求所带来的所有开销。
Whenever applicable you're better off using view cells instead of requesting actions, as they avoid all the overhead that comes with dispatching a new request.
请参见 菜谱>视图>视图单元格
See Cookbook > Views > View Cells
如果没有模型被卷入,而您所做的只是生成HTML,那么您可以简单地使用元素。
In case no models are involed, and all you are doing is generating HTML, then you could simply use elements.
请参见 菜谱>视图>元素
See Cookbook > Views > Elements
一种可能的解决方法是使用分派器过滤器,该过滤器使用必要的属性填充空的 $ base
/ $ webroot
属性值,例如
A possible workaround would be to use a dispatcher filter that fills the empty $base
/$webroot
properties with the necessary values, something like
src / Routing / Filter / RequestFilterFix.php
namespace App\Routing\Filter;
use Cake\Event\Event;
use Cake\Routing\DispatcherFilter;
class RequestFixFilter extends DispatcherFilter
{
public function beforeDispatch(Event $event)
{
$request = $event->data['request'];
/* @var $request \Cake\Network\Request */
if ($request->param('requested')) {
$request->base = '/pro';
$request->webroot = '/pro/';
$request->here = $request->base . $request->here;
}
}
}
config / bootstrap .php
// ...
// Load the filter before any other filters.
// Must at least be loaded before the `Routing` filter.
DispatcherFactory::add('RequestFix');
DispatcherFactory::add('Asset');
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
// ...
另请参见 菜谱>路由>分派器过滤器>构建过滤器
See also Cookbook > Routing > Dispatcher Filters > Building a Filter
这篇关于我该如何在CakePHP 3.x的视图中使用requestAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!