Codeigniter - 如何根据子域是否存在路由控制器 [英] Codeigniter - How to route controller based on whether a subdomain exists or not
问题描述
我在codeigniter中创建一个SaaS Web应用程序,我试图根据是否存在子域来确定如何路由到特定的控制器。
目前我有它,所以如果你放置一个 subdomain.example.com
的URL,我的默认控制器检查
public function __construct() )
{
parent :: __ construct();
$ subdomain_arr = explode('。',$ _SERVER ['HTTP_HOST'],2); //创建各个部分
$ subdomain_name = $ subdomain_arr [0]; // assigns the first part
// echosubdomain_name = $ subdomain_name;
//如果子域不存在,重定向到错误页面
if(!$ this-> subdomainExists($ subdomain_name)){
redirect('error / index ');
} else {
$ this-> subdomain = $ subdomain_name;
}
}
这适用于用户输入子域的网址,但现在如果用户输入没有子域(例如 example.com
)的网址,我想要使用不同的控制器。
什么是实现这个的最好方法?我想是这样做的,但是每次有人去 example.com
时发生重定向似乎并不是最好的。
//如果没有输入子域,重定向到控制器'aDifferentController'
//如果输入了子域,请检查它是否存在于数据库中如果没有重定向到错误页面。
if(输入的URL不包含子域){
redirect('aDifferentController');
} else if(!$ this-> subdomainExists($ subdomain_name)){
redirect('error / index');
} else {
$ this-> subdomain = $ subdomain_name;
}
为什么不有条件地声明基于路由
在routes.php中
< if(strstr($ _ SERVER ['HTTP_HOST'],'some-subdomain.mydomain.com')){
$ route ['uristring'] =controller / method
}
I'm creating a SaaS web app in codeigniter and i'm trying to determine how to route to a specific controller based on whether a subdomain exists or not.
Currently I have it so if you put a url of subdomain.example.com
, my default controller checks whether the subdomain in the url exists in the database, and if it does not, it displays the error page.
public function __construct()
{
parent::__construct();
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2); //creates the various parts
$subdomain_name = $subdomain_arr[0]; //assigns the first part
//echo "subdomain_name = $subdomain_name";
// if the subdomain does not exist, redirect to error page
if(!$this->subdomainExists($subdomain_name)) {
redirect('error/index');
} else {
$this->subdomain = $subdomain_name;
}
}
This works great for urls where a user enters a subdomain, but now if the user enters a url without a subdomain such as example.com
, I want a different controller to be used.
What is the best way to achieve this? I was thinking of doing the following, but it doesn't seem best to have a redirect occuring every time someone goes to example.com
.
// if no subdomain was entered, redirect to controller 'aDifferentController'
// else if a subdomain was entered, check that it exists in the database and if not redirect to an error page.
if(the url entered does not contain a subdomain) {
redirect('aDifferentController');
} else if(!$this->subdomainExists($subdomain_name)) {
redirect('error/index');
} else {
$this->subdomain = $subdomain_name;
}
Why not conditionally declare routes based on the subdomain.
in routes.php, do this
if (strstr($_SERVER['HTTP_HOST'], 'some-subdomain.mydomain.com')) {
$route['uristring'] = "controller/method";
}
这篇关于Codeigniter - 如何根据子域是否存在路由控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!