从CodeIgniter HMVC中的另一个模块加载控制器 [英] Loading a controller from another module in CodeIgniter HMVC
问题描述
我在CodeIgniter HMVC中设置了两个模块。一个是模板,另一个是测试。
I have two modules setup in CodeIgniter HMVC . One is templates and another is test .
这是文件夹结构..
- 模板
- 控制器
- home.php
- 控制器
- ----。php
- 布局
- admin.php
- main.php
- user.php
- 控制器
- test.php
我在routes.php中添加了一个route变量,该变量将home.php路由为模板的默认控制器。和自动加载的模板库。
I have added a route variable in routes.php which routes home.php as the default controller for templates. and auto loaded template library .
现在,当我访问 http://mysite.com/templates/home/index 或 http://mysite.com/templates/ ..它工作正常,但是当我运行另一个模块(测试)时,它显示错误。我还尝试了
echo Modules :: run(’templates / home / index’);
,但是存在相同的问题。我在test.php中有流代码Now when i access http://mysite.com/templates/home/index or http://mysite.com/templates/ .. it works fine but when i run another module ( test ) it shows error . I have also tried
echo Modules::run('templates/home/index');
but same problem . I have the flowing codes in test.php<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Test extends MX_Controller { public function index() { $this->load->module('templates'); $this->templates->index(); } }
它说
无法加载请求的文件:home.php
这是我的模板库
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Template { private $template_data = array(); private $headers = array(); private $CI; public function __construct() { $this->CI =& get_instance(); $this->CI->config->load('template'); } function set($name, $value) { $this->template_data[$name] = $value; } function add_header($header) { array_push($this->headers, $header); } function load($template = '', $view = '', $view_data = array(), $return = FALSE) { $this->CI = & get_instance(); $this->set('contents', $this->CI->load->view($view, $view_data, TRUE)); $this->set('headers', implode('', $this->headers)); return $this->CI->load->view($template, $this->template_data, $return); } } /* End of file Template.php */ /* Location: ./system/application/libraries/Template.php */
推荐答案
似乎可以加载该模块不指定控制器名称,仅当控制器名称与模块名称匹配时:
It seems that the module can be loaded without specifying the controller name only if the controller name matches the module name :
控制器可以作为类加载使用$ this-> load-> module('module / controller')的其他控制器
的变量;或者只是
$ this-> load-> module(’module’);如果控制器名称与
模块名称相匹配Controllers can be loaded as class variables of other controllers using $this->load->module('module/controller'); or simply $this->load->module('module'); if the controller name matches the module name
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview
尝试像这样加载模块:
$this->load->module('templates/home');
这篇关于从CodeIgniter HMVC中的另一个模块加载控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!