Today, we’re going to explore how you can manage multiple applications in the CodeIgniter web framework using a single codebase. In the course of that, we’ll go ahead and create two different CodeIgniter applications that will share the core CodeIgniter codebase.
Sharing the core codebase across different applications is not something new as it’s already practiced by different frameworks and open-source systems, and CodeIgniter is no different. It easily allows you to manage multiple applications that share the core CodeIgniter library and API files, and at the same time you could use different databases and site-specific configurations.
To start with, we’ll go through the benefits of the multisite setup, and as we move on we’ll go through a practical demonstration of what it takes to set up multiple applications in the CodeIgniter framework.
Benefits of the Multisite Setup
In this section, we’ll highlight a couple of benefits of having a multisite setup.
One of the most obvious benefits that I could straightaway point out is that the multisite setup shares a common codebase, and that should make the upgrade and maintenance processes of your application much easier.
For example, let’s imagine that you have ten different CodeIgniter applications running under your belt. And you just came to know that a new version of the CodeIgniter framework is available for upgrade and you would like to upgrade it as soon as possible to make sure that the code remains secure and stable.
If you had a separate codebase for each of your applications, it would definitely be a tedious process to go through each and every site and upgrade it in turn. With the multisite setup, you just need to do it once as the core codebase is shared across all the sites!
Next, it allows you use a different database for each application even though they share a common codebase. In fact, it’s one of the most popular use cases of setting up multisite!
Apart from using a different database for each application, you could create a setup that uses the same database but a different theme or layout in the front-end.
If you are still using the FTP-based approach to move your site files across the different servers, I would say you’re going to love the multisite approach as it minimizes your work to a great extent!
How to Create Multiple Applications
In this section, we’ll set up the basic directory structure in order to implement a multisite setup.
At the root of your CodeIgniter application, create an applications
directory. This is the main directory that will hold our different applications.
Next, go ahead and create two new directories—applications/app_one
and applications/app_two
. Of course, you could name it the way you want it to be, but I’ll keep things simple for now.
So, as you can see, we’re going to set up two different applications that will use the single codebase of the CodeIgniter framework. Although the multisite setup will reuse most of the CodeIgniter framework files, we still need to duplicate a couple of files and directories to each and every application we create.
Let me quickly list the files and directories that you should copy from the default application in the first place.
Copy the following directories from the default application directory to applications/app_one
and applications/app_two
:
- cache
- config
- logs
As you can see, it’s obvious to have separate directories for cache
and logs
for each application. And the config
directory is a must have for the working of your CodeIgniter application, so we are going to copy it anyway.
Next, let’s copy a couple of files along with the necessary directories that allow us to test our multisite application.
Copy the following files to our app_one
and app_two
applications from the default CodeIgniter application:
- controllers/welcome.php
- views/errors
- views/welcome_message.php
For your quick reference, the controllers/welcome.php
file should look like:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { /** * Index Page for this controller. * * Maps to the following URL * http://ift.tt/1aPwdNr * - or - * http://ift.tt/1daeLYa * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /http://ift.tt/1aPwf7Q; * @see http://ift.tt/2j0avVA */ public function index() { $this->load->view('welcome_message'); } }
And the views/welcome_message.php
file should look like.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Welcome to CodeIgniter</title> <style type="text/css"> ::selection { background-color: #E13300; color: white; } ::-moz-selection { background-color: #E13300; color: white; } body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } a { color: #003399; background-color: transparent; font-weight: normal; } h1 { color: #444; background-color: transparent; border-bottom: 1px solid #D0D0D0; font-size: 19px; font-weight: normal; margin: 0 0 14px 0; padding: 14px 15px 10px 15px; } code { font-family: Consolas, Monaco, Courier New, Courier, monospace; font-size: 12px; background-color: #f9f9f9; border: 1px solid #D0D0D0; color: #002166; display: block; margin: 14px 0 14px 0; padding: 12px 10px 12px 10px; } #body { margin: 0 15px 0 15px; } p.footer { text-align: right; font-size: 11px; border-top: 1px solid #D0D0D0; line-height: 32px; padding: 0 10px 0 10px; margin: 20px 0 0 0; } #container { margin: 10px; border: 1px solid #D0D0D0; box-shadow: 0 0 8px #D0D0D0; } </style> </head> <body> <div id="container"> <h1>Welcome to CodeIgniter! You're browsing Application One!</h1> <div id="body"> <p>The page you are looking at is being generated dynamically by CodeIgniter.</p> <p>If you would like to edit this page you'll find it located at:</p> <code>application/views/welcome_message.php</code> <p>The corresponding controller for this page is found at:</p> <code>application/controllers/Welcome.php</code> <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p> </div> <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p> </div> </body> </html>
Of course, you should change the following message in the view file so that we could differentiate the application during the testing.
For applications/app_one/views/welcome_message.php
, it should look like:
<h1>Welcome to CodeIgniter! You're browsing Application One!</h1>
And for applications/app_two/views/welcome_message.php
, it should look like:
<h1>Welcome to CodeIgniter! You're browsing Application Two!</h1>
Now, we have everything done as far as our multisite setup is concerned. However, it won’t work out of the box yet as we still need to inform CodeIgniter about our multisite setup since it always loads the default application located in the application
directory.
Finishing Touches
Let’s have a quick look at the setting that configures the default application directory. Go ahead and open the index.php
file at the root of your application and look for the following code snippet.
/* *--------------------------------------------------------------- * APPLICATION DIRECTORY NAME *--------------------------------------------------------------- * * If you want this front controller to use a different "application" * directory than the default one you can set its name here. The directory * can also be renamed or relocated anywhere on your server. If you do, * use an absolute (full) server path. * For more info please see the user guide: * * http://ift.tt/2tERWbi * * NO TRAILING SLASH! */ $application_folder = 'application';
It’s pretty clear from the above snippet that it allows you to set the path of your default application. So this is the place where we can make changes so that it picks up the default application from a directory other than the default one.
Of course, you could go ahead and straight away do something like this, and that should run the app_one
application.
$application_folder = 'applications/app_one';
On the other hand, what would you do if you want to run app_two
? As a quickie, you could copy the index.php
file to index_app_one.php
and index_app_two.php
for each application. In your virtual host, make sure that you make the changes accordingly.
On the other hand, I prefer a slightly different approach, and I would like to rely on the ENV
variable to choose between the different applications at run time.
For example, you can set up the custom ENV
variable in NGINX as shown in the following snippet.
// set the env variable CI_DEFAULT_APP in the "location" directive of vhost fastcgi_param CI_DEFAULT_APP applications/app_one;
If you’re using the Apache web server, the same could be achieved with:
SetEnv CI_DEFAULT_APP applications/app_one
Next, let’s revise the code in the index.php
file that takes advantage of the ENV
variable to decide the default application to run.
... ... $application_folder = (isset($_SERVER['CI_DEFAULT_APP']) ? $_SERVER['CI_DEFAULT_APP'] : 'application'); ... ...
So, as you can see, we check the existence of the CI_DEFAULT_APP
ENV
variable in the first place, and if it’s not available then we’ll fall back to the default application.
More often than not, you want to run your different applications on different domains. Ideally, I would like to use two different virtual hosts for each application. A quick example of each virtual host should look something like this in the context of NGINX.
The www.ci-app-one.com domain points to app_one
:
server { listen {YOUR_IP}:80; server_name www.ci-app-one.com; root /var/www/html; index index.html index.php; location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; } location / { # Check if a file or directory index file exists, else route it to index.php. try_files $uri $uri/ /index.php; } location ~ \.php$ { root /var/www/html; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; fastcgi_param CI_DEFAULT_APP applications/app_one; include fastcgi_params; } }
Similarly, the www.ci-app-two.com domain points to app_two
:
server { listen {YOUR_IP}:80; server_name www.ci-app-two.com; root /var/www/html; index index.html index.php; location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; } location / { # Check if a file or directory index file exists, else route it to index.php. try_files $uri $uri/ /index.php; } location ~ \.php$ { root /var/www/html; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; fastcgi_param CI_DEFAULT_APP applications/app_two; include fastcgi_params; } }
Of course, you could go ahead now and test your changes to see if it really works or not! Don’t hesitate to shoot me any queries if you face any issues.
And that was the pretty simple way in which you can set up multiple applications in the CodeIgniter framework using a single codebase.
Conclusion
Today, we went through an interesting aspect of the CodeIgniter framework that allows you to manage multiple applications using a single codebase. The obvious benefits of that are easy upgrading and maintenance of your existing codebase.
CodeIgniter is a powerful PHP platform. Whether or not you're just getting started or you're starting with the next version, don't forget to check out what we have available for you, as well.
Share your thoughts if you’ve already implemented something similar or you would have approached it in a slightly different way. Either way, I would love to hear your thoughts!
by Sajal Soni via Envato Tuts+ Code
No comments:
Post a Comment