Codeigniter 路由控制器子文件夹 [英] Codeigniter Routes Controller Sub Folder

查看:23
本文介绍了Codeigniter 路由控制器子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路由和控制器有问题.我有两种类型的控制器:第一种用于管理网页,第二种用于 cms,我更喜欢将它们放在子文件夹中.示例:

I've a problem with routes and controller. I've got 2 types of controller: first type is used to manage the webpages, second type is used for cms and I prefer to put them in a sub-folder. Example:

/controller/site.php (for webpages)
/controller/admin/ (for controllers to manage cms)

在 routes.php 中我写了:

in routes.php I've write:

$route['(:any)'] = "site/$1";
$route['admin/(:any)'] = "admin/$1";

我以这种方式设置了 .htacces 文件:

I've got the file .htacces set in this way:

RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt|css|js|font|woff|ttf|svg|eot|favicon.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

和 config.php 上的这个变量:

and this variable on config.php:

$config['index'] = '';

但它仅适用于站点".例如,如果我写mywebsite/admin/login",它会返回 404 错误.

but it works only for "site". If I write "mywebsite/admin/login" for example, it return 404 error.

我也发现 MY_Router 可以扩展 CI_Route 但不起作用.

I've found also MY_Router to extend CI_Route but doesn't work.

有人可以帮我解决这个问题吗?

Can someone help me to resolve this problem ?

推荐答案

admin 路由放在 any 路由之前:

Put the admin route before the any route:

$route['admin/(:any)'] = "admin/$1";
$route['(:any)'] = "site/$1";

否则它总是会点击any并重定向到site.在匹配 any 之前,你必须给它一个匹配 admin 的机会.

otherwise it will always hit any and redirect to site. You have to give it a chance to match admin before matching any.

这篇关于Codeigniter 路由控制器子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆