【问题标题】:Kohana template controller, no auto renderingKohana 模板控制器,没有自动渲染
【发布时间】:2023-03-03 00:24:01
【问题描述】:

我有一个扩展 Controller_Template 的控制器 (Controller_Product)。 在 Controller_Product 中,我有一些操作(创建、编辑等)需要渲染模板,但某些操作(例如保存、删除)必须返回一个 json 对象,所以我不需要模板被渲染。 我该如何解决这个问题?

我可以在我的 save 或 delete 操作中将 $this->auto_render 设置为 FALSE,但在这种情况下也会创建模板,即使将没有渲染。我认为当我实际上不需要它时加载模板并不是很优雅。

有什么建议吗?

【问题讨论】:

    标签: templates controller kohana


    【解决方案1】:

    也许是这样的:

    public function before()
    {
            if (in_array($this->request->action(), array('save', 'delete')))
            {
                    $this->auto_render = FALSE;
            }
    
            parent::before();
    }
    

    [编辑]

    更好的方法可能是检查 ajax 请求:

    public function before()
    {
            if ($this->request->is_ajax())
            {
                    $this->auto_render = FALSE;
            }
    
            parent::before();
    }
    

    【讨论】:

    • 在大多数情况下检查 ajax 请求对我来说是可行的,因为我每次都通过 ajax 发布数据。感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多