【问题标题】:Connecting `new \Cake\View\ViewBuilder()` to the main `\Cake\Http\ServerRequest`将 `new \Cake\View\ViewBuilder()` 连接到主 `\Cake\Http\ServerRequest`
【发布时间】:2021-07-04 13:17:37
【问题描述】:

我正在通过新的独立 ViewBuilder 将视图模板渲染到变量中。

$builder = new \Cake\View\ViewBuilder();
$builder->setLayout('my_layout');
$builder->setTemplate('my_template');

上面的模板包含一个表单。

echo $this->Form->create(null, array(
    'type' => 'POST',
    'url' => '/',
)).PHP_EOL;
$this->Form->unlockField('my_input');
echo $this->Form->end();

提交后,出现如下错误。

在请求数据中找不到'_Token'。

据我了解,没有添加令牌是因为FormHelper 接收到false when it checks for $this->_View->getRequest()->getParam('_Token'),这是因为这个新的ViewBuilder 没有连接到应用程序操作的实际请求.

有没有办法将我的新ViewBuilder 连接到应用程序的主Cake\Http\ServerRequest

【问题讨论】:

  • 它通常通过 $builder->build(), 2nd arg 传递。你确定你需要创建一个新的 View builder,而不是仅仅在普通 View 的模板中渲染一个 Cell 或 Element?你现在如何渲染这个?
  • @ahoffner,就是这样,谢谢!必须将$this->getRequest() 作为第二个参数传递给viewBuilder::build()。你说得对,这需要重构,但无论如何都必须让它在当前状态下工作。您介意将其发布为答案吗?这样我就可以接受它并关闭问题。

标签: cakephp cakephp-3.0


【解决方案1】:

你可以把它传递给$builder->build,它是第二个参数,你通常可以从$this->getRequest()中得到它。

【讨论】:

    猜你喜欢
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多