私はcodeigniterにSaaS Webアプリケーションを作成していますが、サブドメインが存在するかどうかに基づいて特定のコントローラにルーティングする方法を決定しようとしています。Codeigniter - サブドメインが存在するかどうかに基づいてコントローラをルーティングする方法
現在のところ、subdomain.example.com
のURLを入力すると、デフォルトのコントローラがURLのサブドメインがデータベースに存在するかどうかをチェックし、そうでない場合はエラーページを表示します。
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;
}
}
これは、ユーザーがサブドメインを入力したURLのための素晴らしい作品が、今、ユーザーがこのようなexample.com
などのサブドメインなしのURLを入力した場合、私が使用する別のコントローラをしたいです。
これを達成する最も良い方法は何ですか?私は次のことを考えていましたが、誰かが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;
}
非常に良いアイデアのようです。私はこれで試してみるよ。私はむしろいつもリダイレクトをするより、これをやりたいと思う。 – Catfish
素晴らしい作品。素晴らしいアイデアをありがとう。 – Catfish