Migrating Legancy Laravel 4 project to Laravel 7

Migrating Legancy Laravel 4 project to Laravel 7

General steps to follow:

1New laravel project setup with latest laravel framework version.
2namespace setup for whole app
3composer.json migration
3.1find correct version for all old packages in OLD and add in NEW project.
3.2composer update
3.3solving version confilicts on NEW
3.4for old packages without matched new version, find substitutions and record them. As in the future we need to swap old ones with these new packages.
4configuration file migration.
4.1
5change filters to middlewares
5.1if filter is inside controller, keep middleware at the same place. If filter is in the route, change it to middleware on the same route.
5.2use Response to return response if necessary in middleware.
6refine namespace setup in composer.json. If psr-4 is not possible or may take a long time to implement, use classmap instead. Better not use psr-0.
7copy old public folder to new public folder. do not copy index.php.
8File is replaced with Storage, thus anywhere File is used should be replaced by Storage.

Some specific ugrade/migration tips and tricks:

NoIssueReasonSolutionsphere of influence
1Input class doesn’t supportInput is deprecated.Changed to use Http Request Class
use Illuminate\Http\Request;
$request::all() to get parameters.
Global
2filter doesn’t workfilter is deprecated.changed to use middlewareGlobal, routes
3For All models
extends Eloquent => extends Model
Model
4UserInterface, RemindableInterface doesn’t supportUserInterface, RemindableInterface are deprecated.remove UserInterface, RemindableInterface and related codesUser Model
5can’t get user id The type of primary key UserId in user table is string.In User Model, need to add below
$keyType = ‘string’;
Model
6when testing, pushToGoogleAnalytics() function will cause error:
ErrorException: Cannot modify header information – headers already sent by
comment out this function when you run test cases.test
7controllers moved into new project are using psr-4. And there are folder structure changes.need to define namespaces for each controller and use correct namespace for code inside controller.
8Can’t set Cookie valueCookie queuing is not enabled for api requests by defaultAdd below class in Kernel.php
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
Global
9Rhumsaa\Uuid\Uuid; is deprecated.use Ramsey\Uuid\Uuid;
Rhumsaa\Uuid\Uuid;
10Illuminate\Auth\UserTrait; is deprecated.remove it. use
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
instead.
UserPublicKey model
11use Illuminate\Database\Eloquent\SoftDeletingTrait;deprecated.rename the trait to SoftDeletes.soft deleting.
12Incorrect datetime value: ‘0000-00-00 00:00:00’ for column ‘created_at’mysql’s sql mode settings.solution options:
1) change the sql_mode to allow zero dates, by removing NO_ZERO_DATE and NO_ZERO_IN_DATE. After a restart of MySQL Server, sql_mode variable will be initialized to the setting in my.cnf.
2) change the created column to allow NULL values, and update the existing rows to change the zero dates to null values
UPDATE `users` SET `created` = NULL WHERE `created` = ‘0000-00-00 00:00:00’
3) update the existing rows to change the zero dates to a valid date
UPDATE `users` SET `created` = ‘1970-01-02’ WHERE `created` = ‘0000-00-00 00:00:00’
tables with timestamp ‘0000-00-00 00:00:00’ as default value.
13lists() is deprecated.change to use pluck();

Notice: lists() return array, pluck return collections. If want to return array, have to use pluck()->toArray()
models, sql script
14Error
SELECT list is not in GROUP BY clause and contains nonaggregated column …
In database.php, change strict to false (default true)
‘mysql’ => [
….
‘strict’ => false,
Global database setting
15eloquent query using first() will not return collection, so count() should not be used.remove count() eloquent first() returned result with count() wrapper.
16pluck() doens’t work properlyin Laravel4.2, pluck() returns the first item of the collection while in laravel 5+, pluck() return an array of the collectionchange to pluck()->first()
17for commands migration, 1) Tests\Feature\ArtisanComands\ExportAllTest::testExportAllCommand
ReflectionException: Method exportAll::handle() does not exist
change fire() (old style) to handle() (new style)
18UploadedFile::getClientSize is deprecated since Symfony 4.1UploadedFile::getClientSize is deprecated since Symfony 4.1use getSize() instead.File upload
19//Debugbar::disable(); is deprecated.
//Debugbar::disable(); is deprecated.
in .env configure APP_DEBUG=true to enable it or false to disable it instead. web.php
20route file: filter() is deprecated.route file: filter() is deprecated.change filter() to middleware() web.php
21route file: before is deprecated.route file: before is deprecated.change before to middleware web.php
22HTML farcade is abandoned.HTML and Form is abandoned. install “laravelcollective/html”: “^6.1”, register HTML and Form as new farcade in appServiceProviderall views using HTML and Form farcade.
23blade template with content section cannot be rendered.cannot find $this->layout->content. Controller layout is removed change template extension to the modern way:
1. @extend(‘layout.default’)
2. return View:make() directly.
all views templates.
24middleware CSRF is replaced by “\App\Http\Middleware\VerifyCsrfToken::class”middleware CSRF is replaced by “\App\Http\Middleware\VerifyCsrfToken::class”remove “csrf” middleware. on web.php routes VerifyCsrfToken is enabled by default.all web.php routes.
25Session::clear() is deprecated.Session::clear() is deprecated.use Session::flush() instead.
26Session::set() is deprecated.Session::set() is deprecated.use Session::put() instead.
27Route::group middleware format is changed.Route::group middleware format is changed.use Route::middleware([,])->group() to register middleware on route group.
28array_except() is deprecated array_except() is deprecated e.g use $filtered = Arr::except($array, [‘price’]); instead.
29Paginator farcade is deprecated.Paginator farcade is deprecated.for view composer, use $view->paginator to get the paginator instance.
30Paginator methods are deprecated.getLastPage() => lastPage()
getCurrentPage() => currentPage()
getUrl => url()
getLastPage() => lastPage()
getCurrentPage() => currentPage()
getUrl => url()
all pagination
31Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::withCookie() when downloading file, response should not set cookie.skip $response->withCookie($newCookie); execution.download file
31setBaseUrl() for paginator is deprecated.setBaseUrl() for paginator is deprecated.use withPath() insteadwithPath() for pagiantion.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.