【发布时间】: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