在代码点火器中提供不同的视图 [英] serving different views in code igniter

查看:53
本文介绍了在代码点火器中提供不同的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的代码点火器。
有不同的视图为不同的上下文服务的最佳实践。
例如,我想使用相同的控制器为移动用户代理提供特定的页面。

I'm pretty new to code igniter. Is there best practice for serving different view for different context. For example, I'd like to serve specific pages for mobile user agents with the same controllers.

推荐答案

这不是一个硬的规则。您可以根据需要构建视图文件,并调用 $ this-> load-> view()为控制器中的不同结果加载不同的视图文件。根据我的经验,CodeIgniter非常公开地适应你如何组织你的应用程序的文件。

There isn't a hard rule for this. You can structure your view files however you like, and call $this->load->view() to load different view files for different outcomes in your controller. From my experience, CodeIgniter adapts very openly to how you organize your application's files.

在你的例子中,将我的 system / application / views 文件夹分为两个子文件夹: main 移动设备:

In your example, perhaps I'd divide my system/application/views folder into two subfolders: main for desktop browsers, and mobile for mobile browsers:

system/
  application/
    views/
      main/
        index.php
        some_page.php
        ...
      mobile/
        index.php
        some_page.php
        ...

在控制器的早期部分,代理正在请求它,然后选择 main mobile ,然后根据您的控制器操作相应地显示您的视图。

In an early part of your controller, say the constructor, you can decide what user agent is requesting it and then pick main or mobile based on that, then show your views accordingly from your controller actions.

一些快速的代码片段,可以让你更好的主意,因为你是新的...

Some quick code snippets to give you a better idea since you're new...

// Place this just below the controller class definition
var $view_type = 'main';

// Controller constructor
function MyController()
{
    parent::Controller();

    if ($this->agent->is_mobile())
    {
        $this->view_type = 'mobile';
    }
    else
    {
        $this->view_type = 'main';
    }
}

// Example action
function some_page()
{
    // ...

    // This comes from the 'var $view_type;' line above
    $this->load->view($this->view_type . '/some_page');
}

还有一些有用的参考资料供您探索:

And some helpful references for you to explore:

  • Views in CodeIgniter
  • User Agent Class

希望我的解释有帮助,希望你能用CodeIgniter有乐趣)。

Hope my explanation helps, and hope you have fun with CodeIgniter :)

这篇关于在代码点火器中提供不同的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆