【问题标题】:CodeIgniter load controller within a controller HMVC控制器 HMVC 中的 CodeIgniter 负载控制器
【发布时间】:2012-10-24 08:13:49
【问题描述】:

我正在使用http://github.com/philsturgeon/codeigniter-template/ 作为模板 我试图将其他控制器视图作为部分加载到实际视图中。 我的主要问题是我无法将元数据(javascripts、css)从其他控制器附加到主控制器。

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');
    }
}

转到 url localhost/demo 后,我得到了带有部分的接口,一切看起来都很好,但我发现在我的日志中有些库加载了太多次。

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

我尝试使用 HMVC,但结果相同。

【问题讨论】:

    标签: php codeigniter partial-views codeigniter-2 hmvc


    【解决方案1】:

    我在https://github.com/EllisLab/CodeIgniter/pull/1818 找到了解决方案 有一个真正的 HMVC,它可以工作。我希望这将包含在即将发布的 CodeIgniter 版本中。

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

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


    (来源:github.com/EllisLab/CodeIgniter)

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2011-08-30
      • 2013-07-02
      • 2012-04-18
      相关资源
      最近更新 更多