【问题标题】:How can combine different pages in codeigniter?如何在codeigniter中组合不同的页面?
【发布时间】:2011-09-02 04:44:54
【问题描述】:

假设我将我的页面分成 4 个部分。一个是横幅,一个是登录,一个是主要,另一个是页脚。 Banner 和 Footer 是静态的,不会改变。而登录是基于不同的用户权限或登录状态,最后主要是基于很多事情要做。所以,我有元素的 4 个部分:

http://localhost:8888/my_app/index.php/static_page/banner

http://localhost:8888/my_app/index.php/static_page/footer

http://localhost:8888/my_app/index.php/user/login_fragment

http://localhost:8888/my_app/index.php/system/main_fragment

但是用户只看到一个页面,但是它把所有的员工都组合在了......那么,我不能使用iframe将它们放在一起,我怎样才能让这些页面分离逻辑,但可以组合在一个视图中?谢谢你。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    我想分享我所做的。虽然不是一个原创的想法(不记得我从哪里得到的),但希望这会对你有所帮助。我有一个“包含”[application/views/includes] 文件夹,其中包含以下文件:

    footer.php、header.php、navigation.php 和 template.php

    现在template.php中的代码如下:

    $this->load->view('includes/header'); 
    $this->load->view($main_content); 
    $this->load->view('includes/footer');
    

    关于用法,我举个例子:

    $data = array (
      'page_title'    => 'Users Listing',
      'title'         => 'Users Listing',
      'main_content'  => 'users/showlist',            
    );
    
    $this->load->view('includes/template', $data));
    

    $data 数组中的元素将在视图 [application/views/users/showlist.php] 中以 $page_title、$title 等形式出现。您还可以发送数组或 HTML 内容。

    【讨论】:

    • 我自己也使用了几乎相同的方法,效果很好
    【解决方案2】:

    只需调用load->多次查看

    在你的控制器方法中:

    function index()
    {
     $this->load->view("banner",array(/** some parameters if you want **/));
     $this->load->view("main",array(/** some parameters if you want **/));
     $this->load->view("footer"); // this one doesn't need parameters maybe
    }
    

    查看views 文档页面

    注意:在视图文件本身中(例如在banner.php中),您可以再次调用 load-view 来插入嵌套视图。

    注意2:如果不想马上输出,也可以将load->view的结果返回到一个变量中,方法是添加一个true参数作为第三个参数:

    $login_box = $this->load->view("login",array(),true);
    
    // $login_box contains the view html.
    
    echo $login_box; // or pass it as a parameter to another view if you want
    

    【讨论】:

    • 但我有一些主要逻辑......在控制器中从数据库获取信息,并进行一些检查,我可以直接,它不会加载所有数据......如果我直接调用视图。
    • @ted。不是 100% 确定你的意思,但你应该从数据库中加载信息并在你的控制器方法中进行处理,然后你可以使用第二个参数将一些数据传递给你的视图(查看 Views 文档的链接)
    【解决方案3】:

    首先

    $this->load->view('filename');
    

    可以在同一个控制器函数中多次使用,并且它们会以正确的顺序出现。

    您所做的是分开的控制器部分必须在同一个控制器中使用。我想你有这些文件像

    /application/controllers/static_page.php
    /application/controllers/user.php
    /application/controllers/system.php
    

    而且,如果不对 CodeIgniter 进行大量修改,您就无法从另一个控制器调用一个控制器,而这正是阻止您的原因。如果您必须为许多功能使用相同的处理代码,您必须创建一个helper,您可以在任何地方使用,或者更优雅的解决方案,即在 /libraries/MY_Controller.php 中具有通用控制器功能。

    MY_Controller.php 总是在控制器被调用之前被调用

    <?php
    
    if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class MY_Controller extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            // you can do any kind of processing here
            // in example if you have some view counter
            // this place is always called
        }
    
        function a_generic_function($rawdata)
        {
            // do some processing here
    
            return $data;
        }
    
    
    }
    

    然后,您将能够在您的任何控制器中使用此功能:

    $this->a_generic_function($rawdata);
    

    现在,处理页眉和页脚最聪明的方法是制作一个包含页眉和页脚的“布局”文件。

    这是 layout.php 视图的示例:

    <!doctype html>
    <head>
        <meta charset="utf-8">
        <title><?php echo $title; ?></title>
    </head>
    <body>
        <div id="container">
            <header>This page has been made by <?php echo $user_name; ?>/header>
    
            <div id="main" role="main">
                <?php echo $main_content_view; ?>
            </div>
    
            <footer>Write your footer here</footer>
        </div>
    </body>
    </html>
    

    你看到中间有 $main_content_view。

    您将在控制器中执行此操作:

    // add the HTML for the login to the variable
    // TRUE means we don't output it in the browser, but put it in a String
    $this->viewdata["main_content_view"] = $this->load->view('login', $data, TRUE);
    
    // want to add more data? just ADD it to the variable, like a String
    $this->viewdata["main_content_view"] += $this->load->view('main', $data, TRUE);
    
    // done? send it to the layout, and pass it the $this->viewdata
    $this->load->view('layout', $this->viewdata);
    

    你还能用这个做什么?您可以像这样使用 $this->viewdata:

    $this->viewdata["title"] = 'Function title';
    $this->viewdata["user_name"] = $user_name;
    

    因此您可以像任何其他视图一样编辑您的布局。

    【讨论】:

      【解决方案4】:

      Phil Sturgeon 为 Codeigniter 编写了一个流行的模板库,它将以比目前其他答案中描述的更智能的方式完成您想要的工作。你可以在这里阅读它的文档:http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/

      文档可能需要一段时间才能理解,因为它缺少示例。

      您可以在这里下载模板库:https://github.com/philsturgeon/codeigniter-template

      【讨论】:

        猜你喜欢
        • 2017-10-03
        • 2015-08-01
        • 1970-01-01
        • 2018-03-14
        • 2013-01-24
        • 2011-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多