Upgrade Laravel 4.2 To Laravel 5
Upgrade as follows
- I have a good understanding of Laravel 4.2 and a basic understanding of Laravel 5.0.
- To compare the differences between Laravel 4.2 and Laravel 5.0, learn L5 quickly.
- The program code is not written in disorder and is written according to the basic default rules of Laravel.
- Have enough patience and energy
- 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.
- 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 update
That'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 .env
Copy 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.example
Used 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.php
Toapp/Http/routes.php
Controller controllers
Copy yourcontollers
Toapp/Http/Controllers
. Add the correct namespace to each classApp\Http\Controllers
. Remember to make yourBaseController
Inherit 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 originalmodels
Copy 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.0Middleware
Is a major concern, routingroutes.php
In['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 longerSupportedremember
Please use this methodCache::remember
Program 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;
DeleteUserInterface
AndRemindableInterface
And then addAuthenticatableContract
AndCanResetPasswordContract
These two interfaces.
Add the following twotraits
To the class
use Authenticatable, CanResetPassword;
If you useIlluminate\Auth\Reminders\RemindableTrait
AndIlluminate\Auth\UserTrait
And delete them.
Artisan Commands
Directly copy the file of your command line programapp/Console/Cammands
Directory, and add the corresponding namespace.
Next copystart/artisan.php
Contentapp/Console/Kernel.php
Filecommand
Array. 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/migrations
And then migrate your original database file fromapp/database/migrations
Copydatabase/migrations
.app/database/seeds
Todatabase/seeds
.
This operation does not need to add a namespace becausecomposer.json
This directory has been introduced.
Binding Global IoC Bindings to Global dependency Injection
Ifstart/global.php
If there is an ioc binding, move themapp/Providers/AppServiceProvider.php
Ofregister
Method. You also need to introduceApp facade
.
View template Views
Directly fromapp/views
Copyresources/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/lang
Toresources/lang
Public directory
Copy all your public resources directly!
Test File
Copyapp/tests
Totests
Directory.
Form and HTML Help Functions
If you useForm
OrHTML
The help function iscomposer.json
Add"illuminate/html": "~5.0"
.
Thenconfig/app.php
Add'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.0Beanstalk
Package:"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: