【问题标题】:Routing inside modules Codeigniter hmvc模块内部路由 Codeigniter hmvc
【发布时间】:2016-02-03 15:33:52
【问题描述】:

我想要一个指向每个模块的“全局链接”,如下所示:

application/config/routes.php

里面
$route['search'] = 'searchPage';

如果用户在 url 中输入搜索,它需要转到模块 searchPage,模块内部的路由就在其中。或者这是我的计划,我想要该模块内部的默认路由。

modules/searchPage/config/routes.php

里面
$route['searchPage'] = 'Welcome/showMessage';

使用函数 showMessage 转到 Welcome 控制器。

-modules
--searchPage
---config
----routes.php
---controllers
---views
---models

在我的 Welcome 控制器中:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends MX_Controller {

    public function showMessage()
    {
        $this->load->view('test');
    }
}
?>

版本:Wiredesignz 5.5 和 codeigniter 3.0.3

但这不起作用。有人可以解释为什么这不起作用吗?

我找到了解决办法:

我已将此代码放在 application/config/routes.php 中

$modules_path = APPPATH.'modules/';     
$modules = scandir($modules_path);

foreach($modules as $module)
{
    if($module === '.' || $module === '..') continue;
    if(is_dir($modules_path) . '/' . $module)
    {
        $routes_path = $modules_path . $module . '/config/routes.php';
        if(file_exists($routes_path))
        {
            require($routes_path);
        }
        else
        {
            continue;
        }
    }
}

在 modules/searchPage/config/routes.php 里面

$route['searchPage'] = 'searchPage/Welcome/showMessage';

【问题讨论】:

  • 你是怎么去的?你有什么答案吗
  • @wolfgang1983 我已经在我的帖子中添加了解决方案
  • @da1lbi3 但是你为什么不把这条路由$route['searchPage'] = 'searchPage/Welcome/showMessage'; 放在主(并且应该是唯一的配置目录)路由文件中,就像我在回答中提出的那样?
  • @DavidBm 因为在我看来,它更干净、更合乎逻辑,因为您的模块中的路由引用了该模块。如果我将所有路线都设置在主路线中,它是否混合在一起。所以我认为这个结构已经消失了。

标签: php codeigniter


【解决方案1】:

你的结构表现不佳

-modules
--searchPage
---config
----routes.php
---controllers
---views
---models

应该是这样的

    -config
    --routes.php
    -modules
    --searchPage
    ---controllers
    ---views
    ---models

你只能有一个config文件夹,并且必须在modules文件夹之外,和所有配置的php文件一样

【讨论】:

    【解决方案2】:

    如果你的模块被命名为searchPage

    $route['searchPage'] = 'searchPage/welcome/showMessage';
    $route['some_name'] = 'module/controller/function';
    

    目录示例

    modules >
    
    modules > searchPage >
    
    modules > searchPage > controllers >
    
    modules > searchPage > controllers > Welcome.php
    

    如果您没有使用 htaccess 删除 index.php,您可能需要在您的 url 中包含 index.php。

    使用模块文件夹中的配置和路由,最好使用主application &gt; config &gt; routes.phpapplication &gt; config &gt; config.php 中的一个

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 2015-06-04
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      相关资源
      最近更新 更多