Including Autoloader and Laravel

This week, the challenge was to integrate the front and back ends. The front end code was deployed to our test server. There was a predefined folder structure for the back-end code that I had to copy my PHP code. I kept the Laravel folder structure and just copied across my code into the new structure.

Once we tested the app, the first problem was that the Laravel auto-loader “include” was not working. This was traced to the difference in the directory structure on the test server and the one that Laravel expected.

My initial thought was to change my local folder structure to match the test server. Then, I saw that the production folder structure was going to differ slightly to the test environment. So, it made sense to configure the autoloader “include” statements depending on server environment – local, staging or production.

Laravel has support for configuring different environments, and provides an app->environment() method to give the current environment.

 $environment = App::environment();

So, you can test your environment as follows.

 if (App::environment('staging')){

However, I could not use the Laravel framework in this case, because the auto-loader had to be included in index.php, before the app variable was set up. In fact, the location of start.php (where $app is set up) would be different on each server.

The solution was to test the PHP global, $_SERVER.

 if( $_SERVER['SERVER_NAME'] == 'localhost') {
     // If the environment is local...
     require __DIR__.'/../bootstrap/autoload.php';
     $app = require_once __DIR__.'/../bootstrap/start.php';
 elseif( $_SERVER['SERVER_NAME'] == '') {
     // The environment is staging...
     require __DIR__.'/../staging_server/bootstrap/autoload.php';
     $app = require_once __DIR__.'/../staging_server /bootstrap/start.php';
 elseif ( $_SERVER['SERVER_NAME'] == '') {
     // The environment is production...
     require __DIR__.'/../production_server/bootstrap/autoload.php';
     $app = require_once __DIR__.'/../production_server/bootstrap/start.php';

 | Run The Application

So, in this case, I could not use Laravel’s configuration framework, as Laravel had not yet started. It was time to use plain old PHP.


2 thoughts on “Including Autoloader and Laravel

    1. We are using just one server for test and productions, with different directory structures separate the test and production versions, so while this is the case we will need two directory structures.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.