好的,所以在对我的原始帖子发表评论后,将我指向堆栈上的另一篇帖子,我想出了一种处理我的问题的好方法。它不完全是在链接中找到的答案,而不仅仅是基于逻辑的衍生品。由于我有多个子域,我想推出每个具有自己的一组功能和需求,以及特定于其原因的控制器,这些控制器只能从这些子域调用。
这就是说我的解决方案,对于那些可能偶然发现它的人来说,routes.php
我最终制作了一个小函数来HTTP_HOST
根据我的需要进行拆分.
并从那里使用它。我的例子如下。
请注意,我还替换了 routes.php 中的所有内容,因此它不仅仅是一条直线$route['this/that'] = 'dir/controller';
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/
function whichSubRoute()
{
$subs = array(
"api"=>"api/",
"m"=>"m/"
);
$curr = $_SERVER['HTTP_HOST'];
$curr = explode('.', $curr);
if(array_key_exists($curr[0], $subs))
{
return array($curr[0], $subs[$curr[0]]);
}
return false;
}
//due to the the way this setup works, some controller references
//can be found multiple times (and in no particular order).
//also note due to this setup, each method has its own default and 404
$choiceRoute = whichSubRoute();
if($choiceRoute !== false)
{
if($choiceRoute[0]=="api")
{
$route['default_controller'] = "welcome";
$route['404_override'] = '';
//start version 1 (mvp API)
$route['1.0/user/(:any)'] = $choiceRoute[1].'v1_userinfo/index/$1';
//controllers outside of "/api"
}
if($choiceRoute[0]=="m")
{
$route['default_controller'] = "welcome";
$route['404_override'] = '';
//start version 1 (mobile)
$route['welcome'] = $choiceRoute[1].'m_welcome';
$route['dashboard'] = $choiceRoute[1].'m_dashboard';
$route['user/(:any)'] = $choiceRoute[1].'m_userinfo/index/$1';
$route['reg'] =
//controllers outside of "/m"
$route['login/auth'] = 'login/auth';
$route['logout/mobile'] = 'logout/mobile';
//end version 1 (mobile)
}
}
else
{
$route['default_controller'] = "welcome";
$route['404_override'] = '';
}
/* End of file routes.php */
/* Location: ./application/config/routes.php */
另外请记住,我确实需要每个子域的默认控制器和 404 控制器