简体   繁体   English

CodeIgniter子域路由最佳实践

[英]CodeIgniter Subdomain Routing Best Practice

I am soon to redevelop a site and I am wondering what the best practice is for routing CodeIgniter for subdomains. 我很快将重新开发一个站点,我想知道为子域路由CodeIgniter的最佳做法是什么。 My site will have two "sections", a normal user section and a business section. 我的网站将有两个“部分”,一个普通用户部分和一个业务部分。 The user section will live at http://example.com whilst the business section will live at http://business.example.com . 用户部分将位于http://example.com,而业务部分将位于http://business.example.com Currently to route this I am checking for the domain in the routes file and using a different set of routes for each, something on the lines of: 目前要路由这个我在路由文件中检查域并为每个路由使用一组不同的路由,这些路由:

$url = explode('http://', $_SERVER['HTTP_HOST']);

if($url[0] == 'business.example.com') {
  // routes for the "business" section
  $route['default_controller'] = 'business/homepage/index';
} else {
  // all other routes
  $route['default_controller'] = 'users/homepage/index';
}

I have also split my controllers up into two main folders, "business" and "users". 我还将我的控制器分成两个主要文件夹,“业务”和“用户”。

I am just wondering if this is actually the best way to achieve the desired routing in CodeIgniter or if anyone else can suggest a better approach. 我只是想知道这是否真的是在CodeIgniter中实现所需路由的最佳方式,或者是否有其他人可以提出更好的方法。

You can use two "application" folders for each purpose and switch the applications in the index.php file. 您可以为每个目的使用两个“应用程序”文件夹,并在index.php文件中切换应用程序。

Here is the explanation on the official site: 以下是官方网站上的解释:

https://ellislab.com/codeigniter/user-guide/general/managing_apps.html https://ellislab.com/codeigniter/user-guide/general/managing_apps.html

Mr. Taha's above answer is a good solution, but i have developed this with a kind of different method in ci. Taha先生的上述答案是一个很好的解决方案,但我在ci中用一种不同的方法开发了这个。

In both of your folders (the domain and subdomain) in your server you must have the index.php and the .htaccess file. 在服务器的两个文件夹(域和子域)中,您必须具有index.php和.htaccess文件。 You must define your paths to application and system folders of course and you can define a var (like your domain or something usefull for you) which you will use it later everywhere. 您当然必须定义应用程序和系统文件夹的路径,并且您可以定义var(如您的域或对您有用的东西),您将在以后随处使用它。

In your app you can use a _DOMAIN define in index.php for the https://business.example.com domain something like: 在您的应用中,您可以在index.php中为https://business.example.com域使用_DOMAIN定义,例如:

define('_DOMAIN', 'https://business.example.com');

With this _DOMAIN var you can handle routings like: 使用此_DOMAIN var,您可以处理以下路由:

    if (_DOMAIN == 'https://business.example.com') {
    $route['default_controller'] = 'Your/Controller';
}

and almost everything you want or where you want it (Controllers,libraries,etc..). 几乎你想要的一切或你想要的地方(控制器,图书馆等......)。

Don't know if this is the best solution but for me it was SUPER! 不知道这是不是最好的解决方案,但对我来说这是超级! Hope i helped 希望我帮忙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM