Monday, November 27, 2017

How to Manage Multiple Applications in CodeIgniter

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:

And the views/welcome_message.php file should look like.

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:

And for applications/app_two/views/welcome_message.php, it should look like:

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.

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.

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.

If you’re using the Apache web server, the same could be achieved with:

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.

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:

Similarly, the www.ci-app-two.com domain points to app_two:

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