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

Comments

comments