Upgrade Laravel 4.2 to 5.0 and above version

Laravel framework 5.1 and above versions support php7. Laravel 5.x has modified its application structure to make it more easy, robust and developer friendly. You can not directly upgrade Laravel 4.x to 5.1 or above versions. First you need to upgrade 4.x to 5.0 and then only you can upgrade Laravel to 5.1 or above version.

If you have application built in Lravel 4.x and you are looking to upgrade it to newest version of Laravel to get maximum advantages of this modern framework. Here are steps to upgrade.

  • Install Lravel 5.0 – Open terminal (for linux users), go to folder to where you want to install Laravel , then run below commandcomposer create-project laravel/laravel {directory} "~5.0.0" –prefer-dist

    or you can follow official installation documentation https://laravel.com/docs/5.0/installation

  • Remove vendor/compiled.php file
  • then run this command composer update from root folder of project
  • Open config/database.php and set correct database details
  • Move all files from old public folder (Lravel 4.x project files) to root folder of new installation (Lravel 5.0).
  • Correct path in index.php as below
    require __DIR__.'/bootstrap/autoload.php';
    $app = require_once __DIR__.'/bootstrap/app.php';
  • Correct path on bootstrap/autoload.php to
    require __DIR__.'/../vendor/autoload.php';
    $compiledPath = __DIR__.'/../vendor/compiled.php';
  • Copy and paste your old routes.php file into your new app/Http/routes.php.
  • Move all your controllers from app/Controllers into the app/Http/Controllers. add the app/Http/Controllers directory to the classmap directive of your composer.json file.
  • Create app/Models
    add “app/Models” to the classmap directive of your composer.json file.
  • Move your views from app/views to the new resources/views directory.
  • Move your language files from app/lang to the new resources/lang directory.
  • Move your tests from app/tests to the new tests directory.
  • Add “laravelcollective/html”: “~5.0” to your composer.json file’s require section.
  • Edit config/app.php and add this line to the ‘providers’ array:
    'Collective\Html\HtmlServiceProvider',
    for the 'aliases' array:
    'Form' => 'Collective\Html\FormFacade',
    'Html' => 'Collective\Html\HtmlFacade',
  • Pagination (changes in model/controller class files) Replace any calls to $paginator->links() with $paginator->render().

    Replace any calls to $paginator->getFrom() and $paginator->getTo() with $paginator->firstItem() and $paginator->lastItem() respectively.

    Remove the “get” prefix from calls to $paginator->getPerPage(), $paginator->getCurrentPage(), $paginator->getLastPage() and $paginator->getTotal() (e.g. $paginator->perPage()).

  • Run command at terminal
    composer update
  • Filters in route will not work. For that need to create Middleware class for after and before to work. Follow below tutorial to create middleware
    https://laravel.com/docs/master/middleware
  • We need to add below code on top of controller file, you will get coding error, below will help you to fix it.
    use DB
    So that we can use DB::table(‘Tablename’)

    For input data add on top of controller file
    use stdClass;
    use Input;

    to use model in controller add below code on the top of controller file:
    use App\Models\Category;

    To use segment() function
    Add Request $request in function parameter
    and replace
    Request::segment(4); with $request->segment(4);

    Replace View::make with view()->make

    Replace Config:: with config(‘constant.img’);

  • Admin authentication – changes in route .php
    in place of “before=>auth.admin” use “middleware=>auth.admin”
    and for middleware auth need to create Authmiddlewear class.

Now as we migrated project from laravel 4.x to 5.0 , It is ready to upgrade to 5.1 or above version.

To upgrade laravel 5.0 to 5.2

  • Update your composer.json file to point to laravel/framework 5.2.*.

    Add "illuminate/html": "5.2.*",
    "laravel/framework": "5.2.*",

    to require section of your composer.json file.

  • Add “symfony/dom-crawler”: “~3.0” and “symfony/css-selector”: “~3.0” to the require-dev section of your composer.json file.
  • Then in your config/app.php file, add this to your providers array:
    Collective\Html\HtmlServiceProvider::class
    and this to your aliases array:
    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,
  • Run composer update command at terminal

Thats it!

Update: also read Introduction to laravel 5.x application structure

Introduction to laravel 5.x application structure

There are more than 40 PHP frameworks available. The popular frameworks are Cakephp, Zend, Symfony, Codeigniter, Yii etc. In recent times Laravel got very popular among web developers. Currently, Laravel is one of the top PHP frameworks for modern web development.

popular php frameworks
popular php frameworks

Laravel did major changes in its Application structure from 4.x to 5.x.

Application Structure

Laravel 4.X
  • app
    • commands
    • config
    • controller
    • database
    • lang
    • start
    • storage
    • database
    • tests
    • views
    • filters.php
    • routes.php
  • bootstrap
    • autoload.php
    • path.php
    • start.php
  • public
    • css
    • fonts
    • img
    • js
    • locales
    • packages
    • .htacess
    • index.php
    • robots.txt
  • vendor
    • composer.json
    • composer.lock
    • server.php
Laravel 5.X
  • app
    • Console
    • Http
      • Controllers
      • Filters
      • Requests
    • Providers
  • bootstrap
  • config
  • database
    • migrations
    • seeds
  • public
  • resources
    • lang
    • views
  • storage
    • cache
    • logs
    • meta
    • sessions
    • views
    • work

Laravel 5.x Application structure

App directory only contain application core code. App directory contains directories such as console , http , providers.

  • The Console directory contains all of the custom Artisan commands for your application.
  • The Http directory conatins code to handle request entering your application.It conatins controllers, middleware, and form requests.
    – Filters for authentication in laravel 4.x have been converted to middleware , if you want to use filter you can still use in laravel 5.x.
    The old app/models directory has been removed but if you want to use it you can implement.

You can also create other directories and classes as per your requirement inside app directory to extend your application.

Config directory contains all configuration file of application.

Database directory contains your database migration and seeds.

Resources directory contains all your appliactions views , assest and language files.

Storage directory contains compiled blade templates, file based cache , logs etc.

Why Laravel 5.x changed Application structure?

New structure help developers to easily understand and work with framework , as you can see its follows “best practice” that development community follow.

It also implemented advanced objected oriented features like name spacing etc.

New route:cache Artisan command to speed up the registration of your application routes.

Most important key point is Laravel 5.1 and above version is compatible with PHP7.

Update: Also read Read how to upgrade Laravel 4.2 to 5.0 and above version