Hosting & Domaining Forum

Domain Lists => Available Domain Names => Domain Coupons and Offers => Topic started by: jobtardis on Nov 15, 2022, 09:53 AM

Title: How to implement the work of subdomain in Codeigniter?
Post by: jobtardis on Nov 15, 2022, 09:53 AM
To ensure that mysite.com functions properly on mobile devices, I must confirm that the mobile controller is active when loading m.mysite.com.
Title: Re: How to implement the work of a subdomain in Codeigniter?
Post by: EJASNathan on Nov 15, 2022, 10:24 AM
One possible solution is to copy the framework to a subdomain and move the system/ folder to a higher level to make it shared across all domains and subdomains, while only differing the application/ folder for each domain or subdomain.

Another option is to configure the subdomain on the server with DNS and .htaccess, which can be more complex.

Alternatively, to direct m.mysite.com to the main site and route it directly to routes.php, you can perform a check on the subdomain m. in $_SERVER['HTTP_HOST'] and set $route['default_controller'] = "mymobilecontroller" if it's present.

When creating a mobile version of a website, it's important to consider the differences in functionality and design compared to the desktop version. Mobile users have different needs and behaviors, and the site should reflect that to provide the best user experience.
Title: Re: How to implement the work of a subdomain in Codeigniter?
Post by: Hitler on Nov 19, 2022, 01:46 PM
Although I didn't find the solution to my problem on this forum, I would like to share my experience. When encountering a similar issue, I copied the framework to a subdomain and moved the system/ folder to a higher level for all subdomains, or folders instead of systems to make them common. By doing so, I only needed to differentiate the application folder for each subdomain.

If you are still having trouble resolving your issue, it may require further investigation or a different approach. It's important to be persistent in finding a solution and consider seeking additional help from other resources such as technical support or online forums. Collaboration and sharing knowledge can lead to more effective problem-solving and better outcomes.
Title: Re: How to implement the work of subdomain in Codeigniter?
Post by: bewennick on Oct 24, 2023, 04:42 AM
Using subdomains with Codeigniter is not too complex. In general, you'll need to configure your web server to map the different subdomains to different directories in your filesystem, and then use Codeigniter's routing feature to direct requests to the correct resources.

Here's an overview of the steps:

Define Subdomain in DNS Setting:
First, you need to set up your subdomain in your domain registrar's control panel. Your new subdomain needs to have DNS records associated with it that point to your server.

Configure your Web Server:
Secondly, you need to configure your web server (like Apache). This is done in the virtual host file where you specify dоcumentRoot of a specific directory for this subdomain.

An example of this for Apache would look like:

<VirtualHost *:80>
    ServerName subdomain.yourdomain.com
    dоcumentRoot /var/www/html/subdomain_folder

    <Directory /var/www/html/subdomain_folder>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/subdomain_error.log
    CustomLog ${APACHE_LOG_DIR}/subdomain_access.log combined
</VirtualHost>
Reload or restart your Apache service after modifying the configurations.

Codeigniter Configuration:
For Codeigniter, you need to use its in-built routing capabilities. With Codeigniter, you can specify different base URLs, index files, and other settings per environment.
In the config.php file for each subdomain app, be sure to set the $config['base_url'] parameter to match the subdomain:

$config['base_url'] = 'http://subdomain.yourdomain.com/';
Then in your routes.php file, you would need to set up your routes. For example, you might have something like this:

$route['default_controller'] = "main";
$route['(:any)'] = "main/$1";
This would route all requests for the subdomain to the main controller.


Suppose you have multiple subdomains such as user1.yourdomain.com, user2.yourdomain.com, user3.yourdomain.com etc., and you want to load different views or data based on the subdomain accessed.

Now, let's see a more advanced dynamic view and data serving case:

For serving different views or data according to the subdomain, you would first need to identify the subdomain in your application. You could place this code in your index.php file or within one of your controllers:
$url = $_SERVER['HTTP_HOST'];
$tempUrl = explode('.', $url);
$subdomain = $tempUrl
Once you have the subdomain, you can make decisions and serve views based on it. For example, you could load a different view based on the subdomain:
if($subdomain == 'user1') {
    $this->load->view('user1_home');
} else if($subdomain == 'user2') {
    $this->load->view('user2_home');
} else {
    $this->load->view('default_home');
}
Or fetch and serve data based on the subdomain:

$user_data = $this->user_model->get_user($subdomain);
$data['user'] = $user_data;
$this->load->view('user_home', $data);
It's also possible to set dynamic routing based on the subdomain. Create a new route in application/config/routes.php:
require_once(BASEPATH .'database/DB.php');
$db =& DB();
$query = $db->get('users');
$result = $query->result();
foreach( $result as $row )
{
    $route[$row->subdomain] = "controller/method";
    $route[$row->subdomain."/(:any)"] = "$1";
}
This script fetches all subdomains from 'users' table and creates routes for them. Now you can manage those routes dynamically.


Let's dive a bit deeper and consider we have a multi-tenant application where each user has their own subdomain and we want to manage this within a single CodeIgniter application.

For this scenario, you could do something like this:

In the application/config/config.php file, set your base URL dynamically:
$url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$url .= "://".$_SERVER['HTTP_HOST'];
$url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $url;
This will dynamically set your base URL according to the current HTTP protocol and server host.

Create a MY_Controller in the application/core directory as base for all of your other controllers. In this file, you would get the subdomain and make decisions on how to route requests:
<?php
class MY_Controller extends CI_Controller
{
    public $subdomain;
 
    function __construct()
    {
        parent::__construct();

        $url = $_SERVER['HTTP_HOST'];
        $url_parts = explode('.', $url);
        $this->subdomain = $url_parts
    }
}
Any controller that extends MY_Controller will now be aware of the subdomain.

Then in your controllers, you could load different views or data depending on the subdomain like so:
class Welcome extends MY_Controller
{
    public function index()
    {
        if($this->subdomain == 'user1') {
            $data = $this->load_data_for_user1();
            $this->load->view('user1_home', $data);
        } else if($this->subdomain == 'user2') {
            $data = $this->load_data_for_user2();
            $this->load->view('user2_home', $data);
        } else {
            $data = $this->load_default_data();
            $this->load->view('default_home', $data);
        }
    }
}
In this way, you can handle request going to different subdomains. Remember, for this system to work, a wildcard DNS record would need to be added for your domain so that all subdomains route to your CodeIgniter application, and your web server would need to be configured to handle requests to these subdomains correctly.