Tutorial: upgrading to Laravel 5

In February 2015 Laravel 5 was finally released. If you started the development of your application earlier, it’s likely your app is written in Laravel 4. Of course it’s ok to keep using Laravel 4, but it’s much nicer to upgrade to the latest version and use the latest features.

Upgrading to version 5 is actually not that difficult. One of the main differences between version 4 and 5 is the use of namespacing. Also the directory structure has been changed. On the Laravel website, an upgrade guide can be found. This tutorial is based on this guide and extended with information we needed ourselves when upgrading.

Installing Laravel 5

The easiest  way to upgrade is to start from a fresh Laravel installation and then copy all custom code from your old Laravel 4 application. Installation is simple when using composer. Go to your command line tool and write

composer create-project laravel/laravel projectname --prefer-dist 5.0.*

‘projectname’ is obviously the name of the project. Go to the directory where you would like to create the fresh Laravel installation. Next, use the command above and a new directory (projectname) will be automatically generated with the new Laravel installation inside.

Configuration

laravel logoIn the config directory, a lot of configuration settings are defined. The actual values of those settings, for example the database name, username and password, are not defined within those files, but in the .env file in the root. The file is also included in the .gitignore. When you work with multiple developers or development environments, it’s easy to use different settings for each environment.

Routes

Next, it’s time to start copying files to your new Laravel version. The routes.php file used to be located directly in the app folder, but in Laravel 5 this is in the app/http folder. You just have to copy the file.

Controllers

Also the controllers have a new location, they are now in app/Http/Controllers. Copy all your controllers to the new installation. In Laravel 5, the BaseController from which you inherite is replaced by ‘Controller’. At the top of your controllers, this should be changed if you want to use this new one, e.g.:

class CustomersController extends Controller

As mentioned above, Laravel 5 makes use of namespacing. The controller are in the App\Http\Controllers namespace. Therefore, add the following at the top of all your controllers:

namespace App\Http\Controllers

Next, check the code written in each controller. Are you referering to any models or are you using the DB or redirect classes? Then include them at the top, e.g.:

use DB;
use Customer;
use Redirect;

If you don’t want to refer to this classes at the top of the document, you have to refer to the class including its namespace on each function call (e.g. App\DB).

In Laravel 4, we often used constructions like View::make(‘users.login’) to call a view from a controller. This doesn’t work out-of-the-box in Laravel 5. Instead, return view(‘users.login’) can be used, which is actually more readable.

Models

In Laravel 5, models are placed directly in the app directory. They are no longer in a subdirectory ‘Models’. Copy all models from your old installation except your User Model. Laravel 5 comes with a build in user system and if you consider to use this, don’t replace the User Model. Instead, copy only your self-written functions into this new model.

Again, namespacing should be applied. Add the following lines of code at the top of each model:

namespace App;
use Illuminate\Database\Eloquent\model;

and change

class Customer extends Eloquent

into

class Customer extends Model

Views

Views are no longer located in the app directory, but have been moved to the resources directory in Laravel 5. Copy your views to this new location.

If you made use of the Form fascade in Laravel 4, you now get an error (Class ‘Form’ not found). The Form Fascade is no longer included by default in Laravel 5. To keep using this fascade, you have to include it manually. Here is how it works:

Go back to your command line tool and navigate to the root directory of your Laravel 5 installation. Write

composer require illuminate/html

Then go to the config/app.php file and scroll down to the ‘providers array’. Add the following line of code:

'Illuminate\Html\HtmlServiceProvider',

Scroll down further to the ‘aliases’ array and add:

'Form'=> 'Illuminate\Html\FormFacade',
'Html'=> 'Illuminate\Html\HtmlFacade'

Now the Form::open(), Form::close() and other tags you are used to can be used in the view.

The blade syntax has also slightly changed. By default, {{ }} is displaying escaped output. HTML tags will just be printed on your screen. To display the unescaped content, use {!! and !!} to print variables.

For example, you want to create a hidden form input with name ‘id’ and the customer_id as value. If you use:

{{ Form::hidden('id',$customer->id) }}

raw html will be displayed on your screen. Instead you have to use the {!! and !!} to display this as an actual hidden input.

{!! Form::hidden('id',$customer->id) !!}

Javascript, CSS and images

Now it’s time to copy any images, CSS and Javascript files to your new Laravel 5 application. In the ‘resources’ directory, create subdirectories for Javascript, CSS and images and copy the files. Don’t forget to adjust the path’s in your views.

External Libraries

You may be using external php libraries in you application, for example for the generation of pdf files or some helper class. Copy those libraries to your fresh Laravel 5 installation and add the path to the ‘autoload-classmap’ array of the composer.json file. Next, run composer dump-autoload from the commandline to make those libraries working.

Migrations and seeds

Laravel 5 has two migrations by default to create a users table and a migrations table. When you’re upgrading from Laravel 4, your application already has those table, so those migrations should be deleted. Next, copy the migrations to Laravel 5 and copy the contents of the seeder file.

Migrations are still run via the familiar ‘php artisan migrate’ command, but the creation of new migration has slightly changed:

php artisan make:migration

Database seeds are still run via php artisan db:seed

Auth and CSRF filter

The Auth and CSRF filter are included by default in Laravel 5, so there’s no need to copy them. CSRF protection is applied by default on any post request.

Done!

After following all steps above, your application is now running on Laravel 5! If you have any questions, just place a comment below!


Mijn Twitter profiel Mijn Facebook profiel
Leonie Derendorp Webdeveloper and co-owner of PLint-sites in Sittard, The Netherlands. I love to create complex webapplications. I am always busy to learn new backend techniques and I stay on top of ongoing development. Websites and webapplications should be intuitive to use and that is what we take care off. Latest post
Transition to Laravel Forge and Envoyer - part 1

Leave a Reply

Your email address will not be published. Required fields are marked *