【问题标题】:Multiple pages with codeigniter?带有codeigniter的多个页面?
【发布时间】:2009-07-22 16:17:58
【问题描述】:

我试图找到一种最好的方式,只需要加载一次我的视图并在不同的页面上重复使用它们。

例如,对于主页,我有以下代码:

function index()
    {

        /* Load Includes for all pages */
        $header = $this->load->view('includes/header','',true);
        $scripts = $this->load->view('includes/scripts','',true);
        $navigation = $this->load->view('includes/navigation','',true);
        $footer = $this->load->view('includes/footer','',true);

        /* Load widgets for Home Page*/
        $rotator = $this->load->view('widgets/home_feature','',true);

        $login = $this->load->view('widgets/login','',true);

        $cal = $this->load->view('widgets/calendar_home','',true);
        $gallery = $this->load->view('widgets/photos_scroll','',true);
        $tags = $this->load->view('widgets/tags_view','',true);
        $spotlight = $this->load->view('widgets/spotlight','',true);
        $recent = $this->load->view('widgets/activityfeed','',true);

        $data=array(
                    'title'=>'Philly2Night.com',
                    'MetaDesc'=>'Cities2Night new social network',
                    'MetaKeywords'=>'Social, Party, Plan, Events',
                    //Load Includes
                    'header'=>$header,
                    'scripts'=>$scripts,
                    'navigation'=>$navigation,
                    'footer'=>$footer,
                    //Load Widgets
                    'feature'=>$rotator,
                    'login'=>$login,
                    'calendar'=>$cal,
                    'photos'=>$gallery,
                    'tags'=>$tags,
                    'spotlight'=>$spotlight,
                    'recent'=>$recent
                    );
        $this->load->vars($data);           
        $this->load->view('pages/home_view');



    }

如何创建新页面,但引用这些视图?我尝试将 var 设为全局,理想情况下我想使用 switch,并定义案例,但这也不起作用。

【问题讨论】:

    标签: php jquery codeigniter


    【解决方案1】:

    如果我理解正确,我认为您只想为所有加载代码提供一个公共位置,这样您就不必为控制器中的每个操作复制/粘贴该部分。如果是这样的话……

    在您的控制器中创建一个构造函数,将load->views 移动到那里,并将它们存储到类中的变量中:

    function __construct() {
        parent::__construct();
        /* Load Includes for all pages */
        $this->header = $this->load->view('includes/header','',true);
        $this->scripts = $this->load->view('includes/scripts','',true);
        $this->navigation = $this->load->view('includes/navigation','',true);
        $this->footer = $this->load->view('includes/footer','',true);
    }
    

    确保将任何 $header、$scripts 等引用更改为 $this->header、$this->scripts 等。

    另外,如果您使用的是 PHP4,请将 __construct 替换为您的类名。

    【讨论】:

      【解决方案2】:

      首先,您不应该加载这么多页面。每个文件系统请求都会影响性能。而不是有“页眉”和“页脚”,只需将它们组合到“home_view”页面中。

      其次,您的问题听起来像是在说,一旦您加载视图,您希望它们在之后的每个页面请求中保持加载状态。这对于 PHP 是不可能的,因为每个请求都是一个全新的负载。但是,如果您正在执行大量数据库查询或计算,然后加载小部件,您可以将小部件缓存 5 分钟左右并保存每个页面的查询。在 CodeIgniter 论坛中查找缓存类。

      【讨论】:

      • 这样做是为了在我的主页视图的不同位置加载片段。
      【解决方案3】:

      这可能有用也可能没用: http://codeigniter.com/forums/viewthread/77279/

      我在我的所有 CodeIgniter 项目中都使用它,它使模板非常非常容易。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 2013-04-13
        相关资源
        最近更新 更多