At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

Migrate Your Laravel 4 Application To Laravel 5

  • By Vignesh Thandapani
  • October 16, 2017
  • 878 Views

Laravel releases a new version very frequently. Recently I have migrated my laravel application to the latest version and found that it may be little difficult for the beginners. So here in this blog, I am going to provide you some tips which help you to upgrade your Laravel 4 application to Laravel 5. (Both Laravel 4 and 5 are having multiple subversions. Since writing multiple blogs with each possible combination of sub-versions is a bit difficult. In this blog, I’m going to give common information about migrating the major versions 4 to 5.

Before you start, keep your old application in some other directories. And at first, you have to install Laravel 5 application into your machine. Using the below command (I suggest to install via composer) :

composer create-project laravel/laravel –prefer-dist

 

If you want to install by using some other methods, kindly refer the documentation here: https://laravel.com/docs/5.0

Configuration Files :

Your Laravel 4 application configuration files would be inside the “your_old_project_path/app/config/”,’config’ directory, Laravel 5 has some change in the directory structure. So all the configuration files should be moved to “your_new_project_path/config/” directory which is available outside the app directory. And also instead of storing the credentials inside the configuration files, you can use the environment files. The benefit of using environment files is that you can keep the separate credentials for each environment (development, production, testing servers like etc). Here is the sample .env file and how to read the .env file values.

//your_new_project_path/config/api.php
/* It is just some reference how you the implement .env in your configurations files*/
return [
 'app_name'          => env('app_name'),
 'api_key'           => env('api_key'),
 'client_id'         => env('client_id'),
 'client_secret'     => env('client_secret')
];
// .env
app_name='YOUR_APP_NAME'
api_key='API_KEY'
client_id='YOUR_CLIENT_ID'
client_secret='CLIENT_SECRET'

 

Routing :

Laravel 4 application routes are available in “your_old_project_path/app/routes.php”. But in Laravel 5 all the routes that are related to HTTP and these routes are grouped under “your_new_project_path/app/Http” including routes.php. So you have to move routes.php file in there.

Filters :

Laravel 4 having filters but Laravel 5 don’t have filters so instead of filters you have user middleware. The filter should be replaced with middlewares in routes.

/* here the sample middleware example for how to use middleware in routs */
Route::get(‘/users’, [‘middleware’ => ‘auth’, function()
{
   //
}]);

 

In case if there is any custom filter then migrate into the middleware.

I had some third party login like google login middleware in the routes so I will provide the some sample reference for custom middleware.

// your_new_project_path/app/Http/Middleware/GoogleLogin.php
/* This for some sample reference for custom middleware.*/
class GoogleLogin
{
 public function handle($request, Closure $next)
 {
   $ga = \App::make('\App\Services\GoogleLogin');
   if (!$ga->isLoggedIn()) {
     return redirect('login');
   }
   return $next($request);
 }
}
// your_new_project_path/app/Http/Kernel.php
/*Here you can register your custom route middleware */
protected $routeMiddleware = [
       'google_login'  => 'App\Http\Middleware\GoogleLogin',
];
//your_new_project_path/app/Http/routes.php
/* here sampe how to use custom middleware in routs*/
Route::any('/users', ['middleware' => 'google_login', 'as' => 'users', 'uses' => 'GoogleController@users']);

 

By CSRF production added in middleware, So if you no need this Then go to app/Http/Kernel.php and comment the proper line.

Controllers :

Now you need to copy all files from “your_old_project_path/app/controllers/*” to “your_new_project_path/app/Http/Controllers/” and need to update the namespace like “APP\Http\Controllers” and also update the ‘BaseController’ to ‘Controller’ Class

In case you want to change the ‘App’ root name use the below command

php artisan app:name YourAppName

 

Migrations :

Database migration files should be moved from ‘your_old_project_path/app/database/*’ to ‘your_new_project_path/database/’.

Laravel 5 application database directory already having users and password_resets table. So based on your needs delete or update this.

Models :

Laravel 5 don’t have models folders, but Laravel 4 is having it. So you can create ‘Models’ folder inside the ‘your_new_project_path/app/’ folder and move your model files here. And update the namespace “App/Models”, like below

namespace App\Models;
class User extends Eloquent {
 …
}

 

Application Services :

Your old application having any services files in ‘src’ move those files into ‘your_new_project_path/app/Services’ and update the proper namespace

Views :

All the view files moved from ‘your_old_project_path/app/views’ to ‘your_new_project_path/resources/view’

The resources folder contains a folder ‘lang’ for the application localization and a folder ‘ assests’ for the front end assets. Laravel 5 also has Elixir, which adapts the Gulp task runner to the laravel development environment.

I have already explained how to combine CSS and JS files using Elixir in my another blog https://www.agiratech.co.uk/laravel-development-how-to-force-a-browser-to-refresh-css-and-js-files-on-an-update/

Composer :

Check your old project composer.json and move necessary code into your new Laravel application and do the composer update to indicate the changes.

Forms and HTML in laravel 5:

Laravel 4 has a default install the illuminate/html package. But, Laravel 5 don’t install this package by default

So the installation should be done manually using the below code. Add the below line in composer.json

   "illuminate/html": "5.0.*"

 

then run the below command

composer update

 

Then add ‘Illuminate\Html\HtmlServiceProvider’ in ‘config/app.php’. If you want to use Html and From facades in the blade template.

Then append the below code in ‘config/app.php’

'aliases' => [
 'Form'=> 'Illuminate\Html\FormFacade',
 'HTML'=> 'Illuminate\Html\HtmlFacade',
],

 

Conclusion :

Obviously, this is just a sample reference, but based on your application you may face different issues. And the process also may be much longer for your application. This blog explains the most common things to upgrade your application from Laravel 4 to 5.

To learn more about this upgrade process refer https://laravel.com/docs/5.0/upgrade. For any queries or doubts please comment below.

Vignesh Thandapani

An enthusiastic Tech Lead with 6 plus years of experience in Web development arena. Owns legitimate experience in CorePHP, Laravel, Symfony, CakePHP, Wordpress, Joomla. Behalf, a young Aspiring "Travel admirer" craves to live with Nature.