【问题标题】:routes.php file not loading when using hmvc in codeigniter?在codeigniter中使用hmvc时没有加载routes.php文件?
【发布时间】:2012-09-08 21:48:51
【问题描述】:

我已经设置好 hmvc 并且工作正常,

我有一个图库模块。

图库模块分为三个控制器,其结构如下:

/modules/gallery/
/modules/gallery/config/
/modules/gallery/helpers/
/modules/gallery/controllers/
/modules/gallery/controllers/gallery.php
/modules/gallery/controllers/galleries.php
/modules/gallery/controllers/images.php
/modules/gallery/models/
/modules/gallery/models/galleriesmodel.php
/modules/gallery/models/imagesmodel.php
/modules/gallery/views/dashboard.tpl
/modules/gallery/views/galleries/dashboard.tpl
/modules/gallery/views/images/dashboard.tpl

无论如何,我的images.php 控制器中有一个函数,名为list_items

所以我想将 url
http://example.com/gallery/images/list 映射到
http://example.com/gallery/images/list_items

所以我想,甜蜜的, 我将添加一个/modules/gallery/config/routes.php,其中包含路由。

但似乎没有包括路线。

包含来自/application/config/routes.php 的路由,如果我在模块routes.php 中添加die('loaded'),它确实会杀死脚本,

但正在运行

来自其中一个控制器的print_r($this->router) 不显示来自模块routes.php 的任何路由。

这是怎么回事?

【问题讨论】:

  • 你能告诉我们/modules/gallery/config/routes.php的内容吗?
  • $route['gallery/list'] = 'gallery/images/list_items'; $route['gallery/list/(:any)'] = 'gallery/images/list_items/$1';
  • 你有没有试过用我之前写的扩展来调试它? stackoverflow.com/questions/12446184/…
  • 是的,路线没有被击中
  • 如果你清空文件 modules/gallery/config/routes.php。并访问模块yoursite.com/gallery 你应该得到以下错误:application/modules/gallery/config/routes.php does not contain a valid route array 这个错误应该表明文件被加载。如果您没有收到此错误,我们知道它无法识别路由文件。

标签: php codeigniter routing hmvc codeigniter-routing


【解决方案1】:

据我所知

HMVC 仅在每个模块内查找请求的控制器,具有不同的层次模型,但从不读取模块内使用 routes.php 的路由覆盖。

查看MX_Router::locate,它永远不会在任何模块中寻找@​​987654322@。

它也不会覆盖在配置文件夹中寻找routes.phpCI_Router::_set_routing

您必须覆盖 CI_Router::_set_routing 并在每个可用模块中读取 config/router.php 才能使您的模块路由覆盖起作用。

【讨论】:

    【解决方案2】:

    Broncha 给你指路,我给你看代码。 我使用这个解决方案:

    您必须在 system/core/Router.php 中编辑 _set_routing() 或在 MY_Router 类 (third_party/Router.php) 中扩展此方法(更好)。

    现在它应该总是加载module/config/routes.php... (大约在第 140 行)

    // Load the routes.php file.
            if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
            {
                include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
            }
            elseif (is_file(APPPATH.'config/routes.php'))
            {
                include(APPPATH.'config/routes.php');
            }
    
            // Include routes every modules
            $modules_locations = config_item('modules_locations') ? config_item('modules_locations') : FALSE;
            if(!$modules_locations)
            {
                $modules_locations = APPPATH . 'modules/';
                if(is_dir($modules_locations))
                {
                    $modules_locations = array($modules_locations => '../modules/');
                }
                else
                {
                    show_error('Modules directory not found');
                }
            }
            foreach ($modules_locations as $key => $value)
            {
                if ($handle = opendir($key))
                {
                    while (false !== ($entry = readdir($handle)))
                    {
                        if ($entry != "." && $entry != "..")
                        {
                            if(is_dir($key.$entry))
                            {
                                $rfile = Modules::find('routes'.EXT, $entry, 'config/');
                                if($rfile[0])
                                {
                                    include($rfile[0].$rfile[1]);
                                }
                            }
                        }
                    }
                    closedir($handle);
                }
            } 
    
            $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
            unset($route);
    

    希望对你有帮助……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多