Upgrade Laravel5.0 to Laravel4.2

Source: Internet
Author: User
Laravel4.2 upgrade Laravel5.0 phphub. orgtopics474 and later refer to L4 as laravel4.2 and L5 as laravel5.0. We recommend that you upgrade L4 in the following situations to better understand L4. At the same time, you should have a basic understanding of L5 to compare the differences between L4 and L5, quickly learn the code of the L5 program. Follow the basic default rules of Laravel.

Laravel 4.2 upgrade Laravel 5.0 strategy https://phphub.org/topics/474 referred to below L4 laravel 4.2, L5 laravel 5.0 recommended for the following situations to upgrade L4 better understanding, at the same time, I want to have a basic understanding of L5 to compare the differences between L4 and L5, and quickly learn how to write the codes of the L5 program. Follow the basic default rules of Laravel.

Upgrade Laravel 4.2 To Laravel 5.0

Https://phphub.org/topics/474


L4 is laravel 4.2 and L5 is laravel 5.0.

Upgrade as follows

  1. Better understanding of L4 and basic understanding of L5
  2. To compare the differences between L4 and L5, you can quickly learn L5.
  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 L5)

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 L5 project and migrate it again

Create a new L5 project. For the new method, see here. Then copy the L4 file 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 the L5composer.json, Including other code and SDK you reference. However, it should be noted that you should go to the project home page to check whether the author supports or is ready to support L5 for those packages developed for Laravel in sequence. As far as I know, currently, most mainstream packages are supported, because the changes are not very large. After selecting a version that supports L5,composer updateThat's all.

Namespace

The L4 namespace 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 methods in L4: $ myClassName = 'Dog'; $ obj = new $ myClassName (); // In L5, 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. Only this one is available under L5, 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'); // complete namespace needs to be added for L5 }}
Filter Filters

Middleware in L5MiddlewareIs 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

Builder no longer supportsrememberPlease 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 L5database/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 L4{{ }}Corresponding to{!! !!}In L4{{{ }}}Corresponds to{{ }}. 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, L4 is the path string of the paging template passed in links, and the render parameter in L5 is the Illuminate \ Contracts \ Pagination \ Presenter object, you need to create a class that inherits the interface as needed.

Message Queue

TheBeanstalkPackage:"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 characteristics, 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! ^

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.