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
- Better understanding of L4 and basic understanding of L5
- To compare the differences between L4 and L5, you can quickly learn L5.
- 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 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 update
That'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 .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. 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.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'); // complete namespace needs to be added for L5 }}
Filter Filters
Middleware in L5Middleware
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
Builder no longer supportsremember
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 L5database/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 L4{{ }}
Corresponding to{!! !!}
In L4{{{ }}}
Corresponds to{{ }}
. 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, 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
TheBeanstalk
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 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! ^