【问题标题】:hmvc codeigniterhmvc 代码点火器
【发布时间】:2012-06-30 19:02:37
【问题描述】:

我已经学习了 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>

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

我错过了什么吗?

【问题讨论】:

  • 只是为了确认你能看出是在配置中设置的 Modules::$locations 数组吗?
  • 嗯.. 我在配置文件中设置了$config['modules_locations'] = array( APPPATH.'modules/' =&gt; '../modules/', );
  • 已解决... :) 我没有在echo Modules::run('second_module/second_module'); 中包含索引它在代码中正确执行:- echo Modules::run('second_module/second_module\index');
  • 哦,你真的不想从另一个控制器调用一个控制器。这就是为什么它被称为模块化分离。您可以通过与其他模块(first_module/first_module_view.php)引用它们来共享模型和视图。如果你开始从另一个控制器调用一个控制器,你会很头疼。
  • 我倾向于同意 Thom,这就是为什么首先创建 MVC 来分离东西......无论如何,如果你绝对需要这个,不要将 html、head 和 body 标签放入第二个视图,因为这会生成一个语义不正确的网站

标签: php model-view-controller codeigniter module hmvc


【解决方案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)页面的正确方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 2011-06-04
  • 2016-01-28
  • 2012-02-21
  • 2011-03-29
  • 2011-08-06
  • 1970-01-01
相关资源
最近更新 更多