模块内的路由 Codeigniter hmvc.


Routing inside modules Codeigniter hmvc

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

应用程序/配置/路由内部.php

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

如果用户在 URL 中键入搜索,则需要转到模块搜索页面,模块内的路由进入位置。或者这是我的计划,我想要该模块内的默认路由。

模块/搜索页面/配置/路由中.php

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

转到带有函数 showMessage 的欢迎控制器。

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

在我的欢迎控制器中:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends MX_Controller {
    public function showMessage()
    {
        $this->load->view('test');
    }
}
?>

版本: 线设计 5.5 和代码点火器 3.0.3

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

我找到了一个解决方案:

我已将此代码放在应用程序/配置/路由中.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;
        }
    }
}

在模块/搜索页面/配置/路由中.php

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

您的结构表现不佳

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

它应该看起来像这样

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

您只能有一个配置文件夹,并且必须在模块文件夹之外,与配置的所有PHP文件相同

如果您的模块被命名为 searchPage

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

目录示例

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

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

在模块文件夹中使用配置和路由时,最好使用主application > config > routes.php中的配置和路由

,并application > config > config.php