Tuesday, March 22, 2016

Laravel centralized project, multiple entry point directories

Note: This post depends on an htaccess rule and tested only on apache server, I don't know if that applies to other web servers as well
Note 2: This solves routing but still not assets, will update this post when I figure that out 

Long version
I work for a company that develops a web-based ERP system with Laravel as the chosen framework for that matter.
We decided to move to the cloud and start selling online so that meant we should be looking carefully for ways to preserve space and ease future maintenance and updates.

We've decided that we wanted a centralized project-different databases approach, but we also wanted every customer to have their own logo and favicon and specific settings such as language as well as their uploaded files, these kind of things needed to be in their own directories; so, one directory with all the meat, other small directories named after their owners pointing to the centralized project and accessed with a /directoryname .

Short version
What we are trying to do here is having a central directory with all the application logic and some other directories pointing to it containing instance-specific settings.

"Instance" refers to the directories that would point to the central app.
every instance would contain "index.php" and ".htaccess", and any other instance-specific files.

Steps

  • First step was to change the index.php contents in other -instance- directories as that is the entry point for the app when "/directory" is typed as the URL in the browser.
paths to "autoload.php" and "start.php" were changed to point to the central directory, and instance-specific variables such as instance name and its database name were loaded from a file in the instance directory itself.
That loaded the required classes to run the application from the right places but there's still a problem where routes are trying to search for controllers in their directories.
  • Next step is redirecting routes to the central app, in the htaccess file in the instance directories add the following rule 
RewriteRule ^/?${user.dir}/([a-z/.]+)$ central/$1 [R=301,L]
A little clarification:  ${user.dir} is a variable pointing to the current directory that's holding the htaccess file,
central is the name of your centralized app
Example: if your instance name is "dummy" and your centralize app is called "central", assuming a directory structure like this:
www
|-- central
|    |-- app 
|    |    |-- controllers
+-- dummy
 |-- index.php
 |-- .htaccess
 +-- settings.php
 All requests passing through dummy will be redirected to central, so that rule simply replaces the instance name with the central project name.

  • Next is for database configuration, every instance had a settings file that held the database name as a constant, imported in index.php, then in the central app's config/database.php file the database name entry is taking the value stored in that constant.
So in short, these are the files that need to change: index.php, .htaccess in instance directories and database.php in the central project.
That's how you simulate multiple app instances with a centralized Laravel project.

No comments:

Post a Comment