【问题标题】:HMVC With Multilevel Hierarchy Codeigniter具有多级层次结构 Codeigniter 的 HMVC
【发布时间】:2020-03-03 16:56:51
【问题描述】:

我在 codeignter3 中实现 HMVC。我已经在 core 和 third_party 文件夹中添加了所有需要的文件。

我还创建了模块文件夹。在模块内部我创建了前端文件夹 在前端文件夹内部我创建了一个文件夹 test 在里面我创建了 3 个文件夹控制器、模型和视图。在控制器文件夹中,我创建了 Test.php,在模型中创建了名为 Test_model.php 的模型,并在视图中创建了一个文件 index.php

我的控制器代码是

<?php
class Test extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('test_model');
    }

public  function index()
    {

        $data['main_content'] = 'home/index';
        $this->load->view('front/layout', $data);
    }

}
?>

在 application/config/routes.php 中

$route['test'] = "frontend/test/Test/index"; 

当我通过 localhost/myprojectfoldername/test 访问它时,我得到 404

【问题讨论】:

    标签: codeigniter codeigniter-3


    【解决方案1】:

    是的,这会给您一个错误。因为您在模块文件夹中创建了模块文件夹。

    您必须在您的应用程序文件夹中创建模块文件夹,而不是在另一个文件夹内部,路径在 mx_loader application>modules 中定义,因此代码不会被罚款。

    更改其目录并刷新它,以便消除错误。

    更多帮助使用这些链接:

    http://www.dcaulfield.com/how-to-install-codeigniter-hmvc/

    或者您可以直接从我的 gmail 驱动器下载:

    https://drive.google.com/open?id=1Viyo7CQcjJNkBv5ahyiOs5RwwXiOVopg

    下载并设置后,点击网址:

    http://localhost/hmvc/home

    【讨论】:

      【解决方案2】:

      这样使用:

       $route['some-route'] = "yourcontroller/yourmethod";
      

      或者换句话说:

      $route['user'] = 'user/login';
      

      【讨论】:

        【解决方案3】:

        关于 Codeigniter 中 HMVC 模式的一点是,你需要很好地遵循文件夹结构。

        在您的描述中,您说您在控制器中创建了一个 Test.php 文件,该文件位于名为“frontend”的文件夹中,该文件夹也在模块文件夹中的另一个文件夹“test”中。

        现在这里的技巧是,如果你的控制器名称(Test.php)等于文件夹名称(test),你可以简单地这样调用它:

        $route['test'] = "frontend/test/index"; 
        

        而不是这个:

        $route['test'] = "frontend/test/Test/index"; 
        

        另一方面,假设您在名为 User.php 的文本文件夹中创建了另一个文件(或控制器),那么您可以将路由设置为:

        $route['test/user'] = "frontend/test/user/index"; 
        

        然后在您的路由代码示例中,我注意到您尝试将大写字母用于“测试”,这并不重要,您可以只使用小写字母“测试”: $route['test'] = "frontend/test/test/index"; 但请注意,这只是为了解释它是否区分大小写。

        试一试,看看结果

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-30
          • 2013-11-07
          • 1970-01-01
          相关资源
          最近更新 更多