CodeIgniter Routes - 仅从一个类的URL中删除类名 [英] CodeIgniter Routes - remove a classname from URL for one class only
问题描述
我想重新映射到 index.php / services $ c>中的
index.php / pages / services
$ c> 。
I want to remap this: index.php/pages/services
into this index.php/services
.
我该怎么办?
:
$route['(:any)'] = 'pages/';
== UPDATED ==
这种方法有什么动态方法吗?因此,该类中的每个函数都将重新映射,而不需要 url
?
Is there any dynamic approach to this method ? So every functions inside that class would become remap without the classname
in url
?
推荐答案
为了将 www.domain.com/services
映射到页/服务
您会像:
$route['services'] = 'pages/services'
如果您要映射 www.domain.com/whatever
到 pages / whatever
和无论
有几个变体,你有几个控制器, :
If you want to map www.domain.com/whatever
to pages/whatever
and whatever
has a few variants and you have a few controllers, then you would do :
// create rules above this one for all the controllers.
$route['(:any)'] = 'pages/$1'
是,你需要为所有的控制器/动作创建规则,最后一个应该是一个全面的规则,如上所述。
That is, you need to create rules for all your controllers/actions and the last one should be a catch-all rule, as pointed above.
如果你有太多的控制器并且你想解决这个特定的路由,在你的routes.php文件中是安全的:
If you have too many controllers and you want to tackle this particular route, the in your routes.php file it is safe to:
$path = trim($_SERVER['PATH_INFO'], '/');
$toMap = array('services', 'something');
foreach ($toMap as $map) {
if (strpos($path, $map) === 0) {
$route[$map] = 'pages/'.$map;
}
}
注意,而不是 $ _SERVER ['PATH_INFO']
你可能想尝试 $ _ SERVER ['ORIG_PATH_INFO']
或任何组件,给你完整的url路径。 br>
此外,上述没有测试,它只是一个例子,让你开始。
Note, instead of $_SERVER['PATH_INFO']
you might want to try $_SERVER['ORIG_PATH_INFO']
or whatever component that gives you the full url path.
Also, the above is not tested, it's just an example to get you started.
这篇关于CodeIgniter Routes - 仅从一个类的URL中删除类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!