CodeIgniter加载控制器在控制器HMVC内 [英] CodeIgniter load controller within a controller HMVC

查看:248
本文介绍了CodeIgniter加载控制器在控制器HMVC内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 http://github.com/philsturgeon/codeigniter-template/为模板
和我试图加载其他控制器视图作为部分实际视图。
我的主要问题是我不能从其他控制器添加元数据(javascripts,css)到主控制器。



application / core / MY_Loader .php


  class MY_Loader extends CI_Loader 
{
function __construct()
{
parent :: __ construct();
}

函数控制器($ sController){
global $ RTR;

//解析sController字符串ex:demo / index
$ aControllerData = explode('/',$ sController);

$ sMethod =!blank($ aControllerData [1])$ ​​b $ b? $ aControllerData [1]
:'index'
;
$ sController =!empty($ aControllerData [0])
? $ aControllerData [0]
:$ RTR-> default_controller
;

$ sClass = ucfirst($ sController);

$ sPath = APPPATH。 'controllers';

if(!file_exists($ sPath。$ sController。'.php')|| class_exists($ sClass,FALSE)){

set_status_header(503);
exit('无法找到指定的类:'。ucfirst($ sController)。'。php');
}
$ this-> file($ sPath。$ sController。'.php');
$ sClass = new $ sClass;

if(!method_exists($ sClass,$ sMethod)){
set_status_header(503);
exit('没有方法:'。$ sMethod。'in Class:'。ucfirst($ sController)。'。php');
}
$ aArguments = func_get_args();
return call_user_func_array(array($ sClass,$ sMethod),array_slice($ aArguments,1));
}

}


/ *文件结束MY_Loader.php * /
/ *位置:./controllers/MY_Loader。 php * /

application / controllers / demo.php

 <?php if(!define('BASEPATH')){
exit('No direct script access'
}

类Demo扩展CI_Controller
{

函数索引(){

$ this-> > controller('welcome');
$ this-> template-> set_partial('footer','partials / footer') - > build('demo');
}
}

/ *文件结束welcome.php * /
/ *位置:./controllers/demo.php * /



application / controllers / welcome.php

 <?php if(!defined('BASEPATH'))exit('不允许直接脚本访问); 
class Welcome extends CI_Controller {

public function index()
{
echo'Loaded welcome controller';
$ this-> template-> title('Welcome');
$ this-> template-> append_metadata('< script src =/ js / jquery.js>< / script>')
$ this-> template-> set_partial('footer','partials / footer') - > build('welcome_message');
}
}

进入url localhost /

  DEBUG  -  2012年版权所有,保留所有权利。 -10-24 08:02:16  - >数据库驱动程序类已初始化
DEBUG - 2012-10-24 08:02:16 - >用户代理类已初始化
DEBUG - 2012-10-24 08:02:16 - >模板类已初始化
DEBUG - 2012-10-24 08:02:16 - >控制器类已初始化
DEBUG - 2012-10-24 08:02:16 - >已加载文件:/home/www/application/controllers/welcome.php
DEBUG - 2012-10-24 08:02:16 - >数据库驱动程序类初始化
DEBUG - 2012-10-24 08:02:16 - >用户代理类已初始化
DEBUG - 2012-10-24 08:02:16 - >模板类已初始化
DEBUG - 2012-10-24 08:02:16 - >控制器类已初始化
DEBUG - 2012-10-24 08:02:16 - >文件已加载:/home/www/application/themes/default/views/partials/footer.php
DEBUG - 2012-10-24 08:02:16 - >文件已加载:/home/www/application/views/welcome_message.php
DEBUG - 2012-10-24 08:02:16 - >已加载文件:/home/www/application/themes/default/views/layouts/main.php
DEBUG - 2012-10-24 08:02:16 - >助手加载:inflector_helper
DEBUG - 2012-10-24 08:02:16 - >文件已加载:/home/www/application/themes/default/views/partials/footer.php
DEBUG - 2012-10-24 08:02:16 - >文件已加载:/home/www/application/views/demo.php
DEBUG - 2012-10-24 08:02:16 - >已加载文件:/home/www/application/themes/default/views/layouts/main.php
DEBUG - 2012-10-24 08:02:16 - >最后输出发送到浏览器

我试图使用HMVC但结果相同。

解决方案

我在 https://github.com/EllisLab/CodeIgniter/pull/1818
有一个真正的HMVC,它的工作原理。我希望这将包括在即将发布的CodeIgniter版本。



我的测试结果显示在图像中。
controllers / demo.php

  class Demo扩展CI_Controller {

public function index )
{
$ this-> load-> controller('welcome');
$ this-> load-> view('demo');
}
}


I'm using http://github.com/philsturgeon/codeigniter-template/ for the templates and I trying to load other controller view to actual view as a partial. My main problem is that I cant append meta data (javascripts, css) from other controller to the main controller.

application/core/MY_Loader.php

class MY_Loader extends CI_Loader
{
    function __construct()
    {
        parent::__construct();
    }

    function controller( $sController ) {
        global $RTR;

        // Parse the sController string ex: demo/index
        $aControllerData = explode( '/', $sController );

        $sMethod = !empty( $aControllerData[1] )
            ? $aControllerData[1]
            : 'index'
        ;
        $sController = !empty( $aControllerData[0] )
            ? $aControllerData[0]
            : $RTR->default_controller
        ;

        $sClass = ucfirst( $sController );

        $sPath = APPPATH . 'controllers/';

        if ( !file_exists( $sPath . $sController . '.php' ) || class_exists( $sClass, FALSE ) ) {

            set_status_header( 503 );
            exit( 'Unable to locate the specified class: '. ucfirst( $sController ).'.php' );
        }
        $this->file( $sPath . $sController . '.php' );
        $sClass = new $sClass;

        if ( !method_exists( $sClass, $sMethod ) ) {
            set_status_header( 503 );
            exit( 'There is no Method: ' . $sMethod . ' in Class: '. ucfirst( $sController ).'.php' );
        }
        $aArguments = func_get_args();
        return call_user_func_array( array( $sClass, $sMethod ), array_slice( $aArguments, 1));
    }

}


/* End of file MY_Loader.php */
/* Location: ./controllers/MY_Loader.php */

application/controllers/demo.php

<?php if ( !defined( 'BASEPATH' ) ) {
    exit( 'No direct script access allowed' );
}

class Demo extends CI_Controller
{

    function index() {

        $this->load->controller('welcome');
        $this->template->set_partial('footer', 'partials/footer')->build('demo');
    }
}

/* End of file welcome.php */
/* Location: ./controllers/demo.php */

application/controllers/welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {

    public function index()
    {
        echo 'Loaded welcome controller';
        $this->template->title('Welcome');
        $this->template->append_metadata('<script src="/js/jquery.js"></script>');
        $this->template->set_partial('footer', 'partials/footer')->build('welcome_message');
    }
}

After going to the url localhost/demo i got interface with partials and everything looks good, but I see that there are some libraries loaded too many times in my logs.

DEBUG - 2012-10-24 08:02:16 --> Database Driver Class Initialized
DEBUG - 2012-10-24 08:02:16 --> User Agent Class Initialized
DEBUG - 2012-10-24 08:02:16 --> Template Class Initialized
DEBUG - 2012-10-24 08:02:16 --> Controller Class Initialized
DEBUG - 2012-10-24 08:02:16 --> File loaded: /home/www/application/controllers/welcome.php
DEBUG - 2012-10-24 08:02:16 --> Database Driver Class Initialized
DEBUG - 2012-10-24 08:02:16 --> User Agent Class Initialized
DEBUG - 2012-10-24 08:02:16 --> Template Class Initialized
DEBUG - 2012-10-24 08:02:16 --> Controller Class Initialized
DEBUG - 2012-10-24 08:02:16 --> File loaded: /home/www/application/themes/default/views/partials/footer.php
DEBUG - 2012-10-24 08:02:16 --> File loaded: /home/www/application/views/welcome_message.php
DEBUG - 2012-10-24 08:02:16 --> File loaded: /home/www/application/themes/default/views/layouts/main.php
DEBUG - 2012-10-24 08:02:16 --> Helper loaded: inflector_helper
DEBUG - 2012-10-24 08:02:16 --> File loaded: /home/www/application/themes/default/views/partials/footer.php
DEBUG - 2012-10-24 08:02:16 --> File loaded: /home/www/application/views/demo.php
DEBUG - 2012-10-24 08:02:16 --> File loaded: /home/www/application/themes/default/views/layouts/main.php
DEBUG - 2012-10-24 08:02:16 --> Final output sent to browser

I was trying to use HMVC but there was the same result.

解决方案

I found a solution at https://github.com/EllisLab/CodeIgniter/pull/1818 There is a real HMVC and it works. I hope that this will be included in upcoming CodeIgniter release.

My test results are shown in the images. controllers/demo.php

class Demo extends CI_Controller {

    public function index()
    {
        $this->load->controller('welcome');
        $this->load->view('demo');
    }
}

这篇关于CodeIgniter加载控制器在控制器HMVC内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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