Upgrade Laravel 4.2 To Laravel 5

Source: Internet
Author: User

Upgrade Laravel 4.2 To Laravel 5

Upgrade as follows

  1. I have a good understanding of Laravel 4.2 and a basic understanding of Laravel 5.0.
  2. To compare the differences between Laravel 4.2 and Laravel 5.0, learn L5 quickly.
  3. The program code is not written in disorder and is written according to the basic default rules of Laravel.
  4. Have enough patience and energy
  5. Familiar with phpstorm, because it is a large-scale project, and an editor with code logic analysis function will reduce unnecessary errors, especially namespaces and references. If you are not familiar with phpstorm, first check whether it is Awesome in PHPStorm.
  6. Use the plug-in larvel-ide-helper, otherwise phpstorm will be less intelligent. (Note that the generated version of _ ide_helper.php is Laravel 5.0)

The following content is from the official documentation. As I suggest adding all namespaces, the content is different from the document, and some content documents are not mentioned

Create a Laravel 5.0 project and migrate it again

Create a Laravel 5.0 project. For details about how to create a new project, copy the file Laravel 4.2 to the new project.

The copied files include: controller, routes, models, Artisan commands, assets, and some classes or resources you have added.

Composer your dependencies and packages

Copy all the composer dependencies and packages you added to Laravel 5.0composer.json, Including other code and SDK you reference.
However, you need to go to the project home page to check if the author supports Laravel 5.0 or is preparing to support L5. As far as I know, currently, most mainstream packages are supported, because the changes are not very large. After selecting Laravel 5.0,composer updateThat's all.

Namespace

The namespace of Laravel 4.2 is global. Although the official saying that the migration can be performed without namespaces, add them manually! Otherwise, it will be more troublesome in the future. Note: You can use this method to modify the prefix of a namespace:php artisan app:name Yourproj.

If a variable is used as the dynamic class name in your program, be sure to add the complete namespace to the variable:

# Possible format in Laravel 4.2: $ myClassName = 'Dog'; $ obj = new $ myClassName (); // In Laravel 5.0, an error is reported. # In L5, you need to change it to $ myClassName = 'app \ Models \ Dog '; $ obj = new $ myClassName ();
Configuration File

Project root directory command linecp .env.example .envCopy your custom configuration here. The configuration file does not have as many folders as before for you to choose based on the environment. Laravel 5.0 only has this one, this means that you need to customize each environment. However, each project may be different. After writing the configuration file, remember to save the template.env.exampleUsed by other teammates.

Start using under config/env('DB_HOST', 'localhost')To call your configuration to the corresponding array key.

Route routes

Copy the originalroutes.phpToapp/Http/routes.php

Controller controllers

Copy yourcontollersToapp/Http/Controllers. Add the correct namespace to each classApp\Http\Controllers. Remember to make yourBaseControllerInherit the abstract classController. View the file one by one and perform error correction according to PHPstorm prompts, including errors of reference classes and namespaces.

Model models

Create a folderapp/Models, Put the originalmodelsCopy all. First, add a namespaceApp\Models. Then there are some methods associated with other models, such as belongTo and haswon. The first parameter needs to be filled in with the complete namespace, such

Class User extends Eloquent {public function phone () {// return $ this-> hasOne ('phone '); originally, return $ this-> hasOne ('app \ Models \ phone'); // Laravel 5.0 requires a complete namespace }}
Filter Filters

Middleware in Laravel 5.0MiddlewareIs a major concern, routingroutes.phpIn['before' => 'auth']Replace['middleware' => 'auth'].

Also change the filter Filters:

// app/filters.phpRouter::filter('shall-not-pass', function() {    return Redirect::to('shadow');});

This is the case.

// app/Providers/RouteServiceProvider@boot()$router->filter('shall-not-pass', function() {    return \Redirect::to('shadow');});
Cache

BuilderNo longerSupportedrememberPlease use this methodCache::rememberProgram transformation. Ifredis, You also needcomposer require 'predis/predis'.

User Authentication

Follow the steps belowUser model.

Delete the following content

use Illuminate\Auth\UserInterface;use Illuminate\Auth\Reminders\RemindableInterface;

Then add the following code:

use Illuminate\Auth\Authenticatable;use Illuminate\Auth\Passwords\CanResetPassword;use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

DeleteUserInterfaceAndRemindableInterfaceAnd then addAuthenticatableContractAndCanResetPasswordContractThese two interfaces.

Add the following twotraitsTo the class

use Authenticatable, CanResetPassword;

If you useIlluminate\Auth\Reminders\RemindableTraitAndIlluminate\Auth\UserTraitAnd delete them.

Artisan Commands

Directly copy the file of your command line programapp/Console/CammandsDirectory, and add the corresponding namespace.

Next copystart/artisan.phpContentapp/Console/Kernel.phpFilecommandArray. For example

protected $commands = [    'Laracasts\Console\Commands\ClearHistoryCommand',    'Laracasts\Console\Commands\SignupsReportCommand',    'Laracasts\Console\Commands\WelcomeUserCommand',];
Data Migration Database Migrations & Seeds

Delete Laravel 5.0database/migrationsAnd then migrate your original database file fromapp/database/migrationsCopydatabase/migrations.app/database/seedsTodatabase/seeds.

This operation does not need to add a namespace becausecomposer.jsonThis directory has been introduced.

Binding Global IoC Bindings to Global dependency Injection

Ifstart/global.phpIf there is an ioc binding, move themapp/Providers/AppServiceProvider.phpOfregisterMethod. You also need to introduceApp facade.

View template Views

Directly fromapp/viewsCopyresources/views.

In Laravel 4.2{{ }}Corresponding to Laravel 5.0{!! !!}In Laravel 4.2{{{ }}}Corresponding to Laravel 5.0{{ }}. You need to modify it accordingly.

Multilingual file Translation Files

Copyapp/langToresources/lang

Public directory

Copy all your public resources directly!

Test File

Copyapp/testsTotestsDirectory.

Form and HTML Help Functions

If you useFormOrHTMLThe help function iscomposer.jsonAdd"illuminate/html": "~5.0".

Thenconfig/app.phpAdd'providers':

'Illuminate\Html\HtmlServiceProvider',

Then'aliases'Add:

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

Replace$paginator->links()Is$paginator->render(). If you use a paging template, Laravel 4.2 is the path string of the paging template in links. In Laravel 5.0, the render parameter is the Illuminate \ Contracts \ Pagination \ Presenter object, you need to create a class that inherits the interface as needed.

Message Queue

Corresponding to Laravel 5.0BeanstalkPackage:"pda/pheanstalk": "~3.0", No longer"pda/pheanstalk": "~2.1"

Summary

I believe that after you follow the above steps, your program still reports an error. Because your project may have some comparisonsPersonalitySo you need to be careful and patient to complete the error correction.

If you use xdebug for breakpoint debugging, you may get twice the result with half the effort.

If you have any questions, please feel free to discuss them!

Finally, I wish you a level up! ^

Deploy Laravel using Nginx in Ubuntu

Deploying Laravel 14.04 using Nginx on Ubuntu 5.0

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.