hmvc codeigniter

3
我已经学习了CodeIgniter的基础知识,现在正在学习模块。 我的问题:我在模块文件夹中创建了两个文件夹,first_modulesecond_module
first_module文件夹中的控制器中,我的代码是:
<?php
    class First_module extends MX_Controller {
    public function index()
    {
        $this->load->view('first_module_view');            
    }
}
?>

first_module_view 页面代码:

<html>
<body>
    <h1> hey this is first module </h1>
    <?php 
        echo Modules::run('second_module/second_module');
    ?>
</body>
</html>

second_module控制器页面:

<?php
class Second_module extends MX_Controller {
    public function index()
    {
        $this->load->view('second_module_view');
    }
}
?>

second_module_view页面:

<html>
<body>
    <h1> hey this is second module </h1>
</body>
</html>

我试图使用第二模块的控制器部分查看first_module视图中的第二模块视图,但这并没有起作用。
单独地,两个代码都可以正常工作,但Modules::run()似乎不起作用。

我是否遗漏了什么?


1
只是确认一下,Modules::$locations数组在配置文件中设置了吗? - Hardik
嗯...我已经在配置文件中设置了$config['modules_locations'] = array( APPPATH.'modules/' => '../modules/', ); - avinashse
已解决... :) 我在 echo Modules::run('second_module/second_module'); 中没有包含索引。以下代码可以正确执行: echo Modules::run('second_module/second_module\index'); - avinashse
哦,你真的不想从另一个控制器调用一个控制器。这就是为什么它被称为模块化分离。您可以通过引用其他模块(first_module / first_module_view.php)来共享模型和视图。如果您开始从另一个控制器调用一个控制器,您会遇到各种头疼问题。 - Thom
我倾向于同意Thom的观点,这也是为什么MVC首先被创建出来以分离内容... 无论如何,如果你绝对需要这个,不要把html、head和body标签放到第二个视图中,因为那会生成一个语义上不正确的网站。 - Zathrus Writer
请查看:http://stackoverflow.com/questions/13045226/codeigniter-load-controller-within-a-controller-hmvc - FDisk
1个回答

1

我知道您已经找到了解决方案,这并不回答您所问的确切问题,但是,通常我会以以下方式处理:

       <?php
            class First_module extends MX_Controller {
            public function index()
            {
                 $content = array();

                //send content array to 2nd view and return everything as a string to be added as content to the first view

               $content["second_view"] = $this->load->view('second_module_view',$content,true);

               $this->load->view('first_module_view',$content);            
            }
        }
        ?>

首先模块视图变为:

<html>
<body>
    <h1> hey this is first module </h1>
    <?php 
        echo $second_view;
    ?>
</body>

我没有特别使用过模块,但是这是我在控制器和视图中的做法。我会尽量限制从视图中调用模型和模块的次数。只需将控制器中从模型生成的变量传递即可。


不错,这似乎是使用CodeIgniter实现HMVC(而不是简单的MVC)页面的正确方法。 - Matt Mc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接