Kohana框架-Ajax实施最佳实践

发布于 2024-10-30 03:10:29 字数 121 浏览 4 评论 0原文

我正在 Kohana 框架中开发一个应用程序。我想知道在 kohana 中实现 ajax 的最佳实践。到目前为止,我正在使用不同的 ajax 控制器。我认为重要的问题是最大限度地减少资源需求和处理会话。

提前致谢

I am developing an application in Kohana framework. I would like to know the best practices in implementing ajax in kohana. So far I am using different controller for ajax. I think the important concerns will be minimizing the resources requirement and handling sessions.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无声静候 2024-11-06 03:10:29

我正在使用这个:

在Controller_Template中:

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

在我的操作中:

      if ($this->request->is_ajax())    
        {
            ...         
            $this->response->headers('Content-type','application/json; charset='.Kohana::$charset);
            $this->response->body($jsonEncoded);
        }

I'm using this:

In Controller_Template:

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

And inside my actions:

      if ($this->request->is_ajax())    
        {
            ...         
            $this->response->headers('Content-type','application/json; charset='.Kohana::$charset);
            $this->response->body($jsonEncoded);
        }
日暮斜阳 2024-11-06 03:10:29

正如上面的人所说,您的 ajax 操作不需要单独的控制器。您可以利用 Kohana 的请求对象来识别请求类型。这可以通过以下方式完成:

<?php

class Controller_Test extends Controller_Template {
    /**
     * @var     View    Template container
     */
    protected $template = 'template';
    /**
     * @var     View    Content to render
     */
    protected $content = 'some/content/view';
    // Inherited from parent class
    protected $auto_template_render = TRUE;
    public function before()
    {
        parent::before();
        if ($this->request->is_ajax() OR !$this->request->is_initial()) {
            $this->auto_template_render = FALSE;
        }    
    }

    public function after()
    {
        if ($this->auto_template_render == FALSE) {
            // We have ajax or internal request here
            $this->template = $this->content;            
        } else {
            // We have regular http request for a page
            $this->template = View::factory($this->template)
                ->set('content', $this->content);
        }
        // Call parent method
        parent::after();
    }
}

虽然该示例非常简单,但可以将其改进为您想要归档的内容。基本上我已经完成了编写自己的 Controller_Template 来完成我需要的事情。此外,您还可以考虑将 format 参数添加到您的 url 中,以便 .html url 返回数据的常规 html 表示,而 .json url 执行相同的操作,但采用 json格式。
有关更多信息(以及可能的想法),请参阅 kerkness 非官方 Kohana wiki

As the fellas above said, you don't need a separate controller for your ajax actions. You may take advantage of Kohana's request object to identify the request type. This may be done in the following way:

<?php

class Controller_Test extends Controller_Template {
    /**
     * @var     View    Template container
     */
    protected $template = 'template';
    /**
     * @var     View    Content to render
     */
    protected $content = 'some/content/view';
    // Inherited from parent class
    protected $auto_template_render = TRUE;
    public function before()
    {
        parent::before();
        if ($this->request->is_ajax() OR !$this->request->is_initial()) {
            $this->auto_template_render = FALSE;
        }    
    }

    public function after()
    {
        if ($this->auto_template_render == FALSE) {
            // We have ajax or internal request here
            $this->template = $this->content;            
        } else {
            // We have regular http request for a page
            $this->template = View::factory($this->template)
                ->set('content', $this->content);
        }
        // Call parent method
        parent::after();
    }
}

Though the example is very simple it may be improved to what you want to archive. Basically I've finished up writing my own Controller_Template to do the stuff I need. Also you may consider adding the format parameter to your urls so that the .html urls returned the regular html representation of the data and .json urls did the same, but in json format.
For more information (and probably ideas) see kerkness unofficial Kohana wiki

浪菊怪哟 2024-11-06 03:10:29

您实际上并不需要单独的控制器,您也可以使用 Kohana_Controller_Template 来处理 AJAX 请求。

由您决定 AJAX 请求(或子请求,通常是相同的)时的响应是什么。我通常只有在请求是第一个请求(而不是ajax)的情况下才实际渲染模板,否则渲染它是 $content var。

此外,您还可以轻松检查请求是否是 AJAX/子请求:

if ($request->is_ajax())
if ( ! $request->is_initial()) 

You don't really need a separate controller, you can use Kohana_Controller_Template for handling AJAX requests as well.

It's up to you to decide what will the response be in case of AJAX request (or subrequest, it's usually the same). I usually have the template actually rendered only in case of request being the initial one (and not-ajax), rendering it's $content var otherwise.

Also, you can easily check if a request is AJAX / subrequest:

if ($request->is_ajax())
if ( ! $request->is_initial()) 
青瓷清茶倾城歌 2024-11-06 03:10:29

另外,如果使用 Kohana_Controller_Template 作为控制器父级/祖先,最好记住在通过 AJAX 访问时禁用自动渲染,以防止加载和渲染整个模板。

if ($request->is_ajax()) $this->auto_render = FALSE;

Also, if using Kohana_Controller_Template as your controller parent/ancestor it is good to remember to disable auto-rendering when accessing via AJAX, to prevent loading and rendering whole template.

if ($request->is_ajax()) $this->auto_render = FALSE;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文