繁体   English   中英

处理CodeIgniter中的动态内容(最佳实践)

[英]Handling Dynamic Content in CodeIgniter (Best Practice)

我正在使用CodeIgniter创建一个网站,并对最佳做法有疑问。

我通常将我的网页分成模块化块。 标题,内容,页脚(以及其他一些根据需要洒在其中的东西。

'Header'和'Footer'块通常是静态的,而其间的内容可以根据一些变量(例如实际页面 - home,about,contact等)而改变。

下面是页面的控制器。 header_view,navigation_view和footer_view(很可能)永远不会改变。 home_main_view会。

public function index()
{
    $this->load->view('header_view');
    $this->load->view('navigation_view');
    $this->load->view('home_main_view');
    $this->load->view('footer_view');

}

例如,如果用户导航到about页面,则视图可能如下所示:

public function index()
{
    $this->load->view('header_view');
    $this->load->view('navigation_view');
    $this->load->view('about_view');
    $this->load->view('footer_view');

}

我如何处理CI的变化? 是否最好使home_main_view成为变量并将pass传递给index()函数? 之前我已经完成了这个(用户单击一个链接,该链接触发控制器中的一个函数,该函数设置调用index($ var)调用view($ var)的$ var。

我最喜欢的方法是使用一个布局(它只是一个视图),我用它来加载其他组件。 对于您拥有的每种类型的页面,您显然需要一种布局,但使用布局会使以后的更改变得容易。

我更进一步,在我的自定义控制器MY_Controller中创建了一个方法,用于加载布局:

<?php
class MY_Controller extends CI_Controller{
    /**
     * Global $data variable holder
     * @var stdClass
     */
    protected $data;
    /**
     * The templates path, used by the 'render' method to determine the template to load
     * This is the 'root' of the templates folder for a specific section
     * Must NOT contain a trailing slash!
     * @var string
     */
    protected $templates_path = 'site';
    /**
     * The layout used by the 'render' method to load the specified view
     * @var string
     */
    protected $layout = 'default';
    /**
     * Class initialisation
     */
    function __construct() {
        parent::__construct();
        /*init the $data variable*/
        $this->data = new stdClass();
        /*the base url shortcut - to be used where needed*/
        $this->data->base_url = $this->config->item('base_url');
        /*page encoding header*/
        $this->output->set_header('Content-Type: text/html; charset=utf-8');
    }
    /**
     * Helper method that will load a view using the layout configured
     * @param type $view
     */
    protected function render($view = FALSE){
        $this->data->page_template = $this->templates_path.'/'.$view;
        $this->data->templates_path = $this->templates_path;
        $this->load->view($this->templates_path.'/layouts/'.$this->layout, $this->data);
    }
}

然后,在我的布局中,我将$page_template变量中指示的视图加载到适当的位置

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html>
    <head>
        <?$this->load->view($templates_path.'/sections/head')?>
    </head>
    <body class="<?=$page_class?>">
        <?$this->load->view($templates_path.'/sections/header')?>
        <?if(isset($hpslider) && $hpslider)
            $this->load->view($templates_path.'/sections/hp_slider')?>
        <div class="pcontent">
            <div class="page">
                <?=$this->load->view($page_template);?>
            </div>
        </div>
        <?$this->load->view($templates_path.'/sections/footer')?>
    </body>
</html>

希望这可以帮助你。

我不使用CI,但您可以使用$ _post,$ _get或segment让操作知道您要加载的页面。

段示例:当你转到example.com/index.php/controller/index/home时,它会加载“home”视图。

public function index(){
$page=$this->uri->segment(3);
$this->load->view('header_view');
$this->load->view('navigation_view');
$this->load->view($page);
$this->load->view('footer_view');
}

http://codeigniter.com/user_guide/libraries/uri.html

在CodeIgniter中标题为Header and footer的SO主题中查看我的答案。 这就是我处理所有观点的方式。 这是一种相当模块化的方法 - 希望它有所帮助!

评论后更新

例如,主页可能会像这样加载:

class Home extends CI_Controller{
    function index(){
        $d['v'] = 'home';

        //do database interaction here and 
        //ultimately assign results to some key in $d too.

        $this->load->view('init', $d);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM