SaaS application development using PHP in Zend Framework

Software as a Service (SaaS) is a Software distribution model on which the data and associated files will be kept on the Main Server. More details about this can be find from the article named SaaS application development. On that article I mentioned about the common things which we need to consider while developing any SaaS application.

Steps for SaaS application development

On this article, I would like to provide the steps for developing an application in SaaS using PHP in Zend Framework. Hope you have good knowledge in Zend Framework and its usage. R4Resorts.com is the website which we developed using this model. This is a resorts listing website with two main sections:

  • Main Website or R4Resorts.com
  • Resorts Profile or Tenant

We can use one or two Zend installations for developing a SaaS application. On this example, I am explaining the application with Two Zend installations. The first Zend will handle the Main Server works and the second will handle the Tenant or Resorts Profiles & associated features. You can install the first one on the Root Path (public_html) and the other one inside a directory named resort which is located in “public_html/resort”

Main Website or R4Resorts.com

The main website handles the Resorts or Tenants Registration. When each Resort or Tenant register on the site, the details will be stored on the main tenant or resort table like the format:

 

ID NAME SUB_DOMAIN_URL RESORT_STATUS
1 Demo Resorts demo.r4resorts.com Active
2 Hawaii Beach Resorts hawaii.r4resorts.com InActive

The other works on the Main Server are the Searching, Static Pages etc for the site.

Resorts Profile or Tenant

All the main works are implementing this Tenant Profile. An example of active Resort Profile is Demo.R4Resorts.com This a virtual sub domain and it represent a Tenant or Resort Profile. Here are the main steps for implementing this:

1. Install Zend

The very first work is to install Zend for handling the Resort Profile or Tenant Works. You can do this in the “public_html/resort” directory or the directory you plan to use for the Tenant works. After installation check whether the installation works perfectly by visiting the url: “site.com/resort” The installation needs minimum two controllers and are IndexController, to show the Welcome Message and PagenotfoundController, to show the message if some error occurred.

2. Setting up Virtual Sub Domains

Resort_Name_1.site.com, Resort_Name_2.site.com etc are the Sub Domains. To set up this kinds of virtual sub domains, open your CPanel and do the following:

  • Login to CPanel
  • Click on “Subdomains” under “Domains” section
  • Enter * in the subdomain text box and choose your Main Domain Name
  • Enter the directory name where you want to redirect all Sub Domain Request. In our case, enter “resort”. If you leave the directory name, it will redirect to the root site. If you are planning to implement the SaaS with single Zend installation, you must leave this text box as blank.
  • Click on create

Virtual Subdomains Settings in CPanel

To check whether this sub domains works, type any subdomain like test.site.com or xxx.site.com and you can see the Welcome Message from IndexController.

3. Authenticate Tenant

Someone entered any sub domain url, the request will be redirected to Zend Installation on resort directory. We need to Authenticate this request to ensure whether the Sub Domain is a valid one or not. I am using an ACL Plugin to check whether a requested resource is existing or not in the Zend works. And I have added this Tenant Authentication on the top of this plugin as the first work like this:

0
1
2
3
45
6
7
8
910
11
12
13
1415
16
17
18
1920
21
22
23
24
<?php
/*
 *  File Name: Acl.php
 *  File Directory: /library/My/Controller/Plugins
 * *  Access Control Implementation for the Social Network Platform
 */
 
class My_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$tenant_model                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             =                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         new Model_Tenant();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          if($tenant_model->authenticateTenant($_SERVER['HTTP_HOST']) == FALSE):
 $this->_response->setRedirect('http://site.com/pagenotfound'); 
 
return true;
endif;
// End of Authentication validation. 
// Continues the ACL Plugin Code...
 
}
}

The Model Function authenticateTenant check the existance of the Sub Domain URL in the Tenant Table which I mentioned above and returns the status of True or False. It is better to store the Tenant related details to SESSION like Sub Domain Name, URL etc for using in all other places.

Note: You can store the Main Site URL in a Registry and use there.

The above work will either Authenticate the Tenant to continue processing or redirect to Page Not Found in Main Website.

4. Site URL Plugin

We can create a View Plugin for storing the Site URL for using in the Tenant Zend installation. We need to save the Virtual Sub Domain inside this. In a normal website, all requests are based on the same url but in a SaaS application with Virtual Sub Domains, the base url will change depends on the Tenants. To achieve this, I have created a Custom View Plugin and is below:

0
1
2
3
45
6
7
8
class My_Views_Helpers_GetSiteUrl extends Zend_View_Helper_Abstract
{
 
    function getSiteUrl()
    {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          return 'http://'.$_SERVER['HTTP_HOST'].'/';
    }
 
}

You can use this View Plugin on Controller and View pages for getting the current Tenant URL ie current sub-domain url.

5. Other Changes

Some of the other things which needs to consider are below:

  • Keep TENANT ID in all database tables.
  • Use TENANT ID from SESSION in all SELECT Queries for reading the Tenant Specific information.
  • Use the getSiteURL plugin always for redirection.
  • Store the actual path of the resort directory in registry. This is needed while file uploading.

Once you completed all these steps, your SaaS application will starts working in Virtual Sub Domains.

One Response to SaaS application development using PHP in Zend Framework

  1. Eric Lewis says:

    In your article, I would like the steps for developing an application in SaaS using PHP in Zend Framework. It’s a good knowledge for me.