首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeIgniter HMVC路由

CodeIgniter HMVC路由
EN

Stack Overflow用户
提问于 2014-07-18 09:40:14
回答 1查看 2.4K关注 0票数 0

目前正在使用HMVC开发CodeIgniter应用程序。

HMVC应用程序有自己的routing.php,其中包含

代码语言:javascript
复制
$route['finance/bill/add'] = 'finance/show_addBill';

$route['finance/transaction/add'] = 'finance/show_addTransaction';

$route['finance/(:any)'] = 'finance/$1';

$route['finance'] = 'finance';

应用程序有一个财务控制器。当去

代码语言:javascript
复制
http://localhost/finance** it goes to **public function index(){}

http://localhost/finance/transaction/add DOES NOT go to **public function show_addTransaction() {}

http://localhost/finance/addTransaction DOES goes to **public function show_addTransaction() {}

我不明白为什么上述路线不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-18 13:35:02

您不应该在HMVC应用程序中定义路由(这是一条非常强大的经验法则-有例外,但这很少见)。

您应该有一个文件夹结构,如:

代码语言:javascript
复制
Modules
- Finance
- - Controllers
- - - finance //should have index, add and an other generic functions.
- - - transaction // transactions specific functions
- - - bill // bill specific functions.

路由是自动的--按照以下思路:

url/finance ->寻找Modules/Finance/Controllers/Finance/Index()

url/finance/bill ->它会先找Modules/Finance/Controllers/Finance(.php)/Bill()然后再找Modules/Finance/Controllers/Bill(.php)/index()

因此,对于您的场景,您应该拥有:

代码语言:javascript
复制
$route['finance/bill/add']

bill.php控制器--带有class bill --具有add方法的控制器

代码语言:javascript
复制
$route['finance/transaction/add']

transaction.php控制器--带有class transaction --具有add方法的控制器

代码语言:javascript
复制
$route['finance/(:any)']

不存在--正如我所说的,URL路由是自动的,所以只要您有相关的控制器和方法,就可以找到

代码语言:javascript
复制
$route['finance']

简单的finance.php控制器与index方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24821860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档