简体   繁体   English

将 Laravel 5.0 升级到最新版本 (7.x)

[英]Upgrading Laravel 5.0 to the latest version (7.x)

I have been assigned with an old Laravel project (version 5.0).我被分配了一个旧的 Laravel 项目(版本 5.0)。 The first task is to upgrade the Laravel from version 5.0 to the latest version 7.x (as of today).第一项任务是将 Laravel 从版本 5.0升级到最新版本 7.x (截至今天)。

From the Laravel docs, i found out that it's gonna take a painful long process!从 Laravel 文档中,我发现这将需要一个痛苦的漫长过程! I know that there's a paid service called Laravel Shift , that will involved quite a huge money as this is huge version jump.我知道有一个名为Laravel Shift的付费服务,这将涉及相当大的资金,因为这是一个巨大的版本跳跃。 So, Laravel Shift is not an option for me.所以,Laravel Shift 对我来说不是一个选项。

Can I install the latest version of Laravel, and copy and paste all the MVC files from the old version?我可以安装最新版本的Laravel,然后从旧版本复制粘贴所有MVC文件吗? has anyone done this method and succeed right away?有没有人做过这种方法并立即成功?

Thanks.谢谢。

First , let's upgrade php to at least 7.2.5 Given below commands are for 7.4首先,让我们将 php 升级到至少 7.2.5 鉴于以下命令适用于 7.4

sudo apt-get update
sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt -y install php7.4
sudo apt-get install -y php7.4-{bcmath,bz2,intl,gd,mbstring,mysql,zip,xml,curl,json}
php -v
sudo update-alternatives --set php /usr/bin/php7.4
sudo a2enmod php7.4
sudo systemctl restart apache2

For verification, check {url}/phpinfo.php如需验证,请查看 {url}/phpinfo.php

Second , remove composer.lock file & vendor folder rm -R vendor .其次,删除composer.lock文件和供应商文件夹rm -R vendor Run composer install .运行composer install

Third , and this is where everything goes to HELL!!第三,这就是一切都变得地狱的地方! Changes in code after upgrade升级后的代码变化

1) The has Method 1) has 方法
The $request->has method will now return true even if the input value is an empty string or null.即使输入值为空字符串或 null,$request->has 方法现在也将返回 true。 A new $request->filled method has been added that provides the previous behaviour of the has method.添加了一个新的 $request->filled 方法,它提供了 has 方法的先前行为。
eg例如

array:1 [
  "class_teachers" => null
]

$request->has('class_teachers')
true

$request->filled('class_teachers')
false


2) Remove html special characters from html page title. 2)从 html 页面标题中删除 html 特殊字符。


3) Nested Ternary Operations 3) 嵌套三元运算
Must explicitly use parentheses to dictate the order of the operations.必须明确使用括号来指示操作的顺序。

1 ? 2 : 3 ? 4 : 5;       // deprecated
(1 ? 2 : 3) ? 4 : 5;     // ok
1 ? 2 : (3 ? 4 : 5);     // ok


4) The or Operator 4) or 运算符
The Blade "or" operator has been removed in favor of PHP's built-in?? Blade "or" 运算符已被移除,取而代之的是 PHP 内置的?? "null coalesce" operator “空合并”运算符

$user->name or "-"  -->   $user->name ?? "-"


5) The Input Facade 5) 输入门面

'Input' => Illuminate\Support\Facades\Input::class,  -->  'Input' => Illuminate\Support\Facades\Request::class,


6) Array and string offset access syntax with curly braces is deprecated 6) 不推荐使用带有花括号的数组和字符串偏移访问语法

$str = "test";
echo $str{0};  // deprecated
echo $str[0];  // ok


7) withCount Column Formatting 7) withCount 列格式
When using an alias, the withCount method will no longer automatically append _count onto the resulting column name.使用别名时,withCount 方法将不再自动 append _count 到结果列名称上。


8) Logging 8) 记录
All logging configuration is now housed in its own config/logging.php configuration file.所有日志记录配置现在都位于其自己的 config/logging.php 配置文件中。 You should place a copy of the default configuration file in your own application and tweak the settings based on your application's needs.您应该将默认配置文件的副本放在您自己的应用程序中,并根据应用程序的需要调整设置。

The log and log_level configuration options may be removed from the config/app.php configuration file.可以从 config/app.php 配置文件中删除 log 和 log_level 配置选项。

use Illuminate\Support\Facades\Log;
Log::info('Showing user profile for user: '.$id);


9) Queue 9) 排队
The QUEUE_DRIVER environment variable has been renamed to QUEUE_CONNECTION . QUEUE_DRIVER环境变量已重命名为 QUEUE_CONNECTION This should not affect existing applications that you are upgrading unless you intentionally modify your config/queue.php configuration file.这不会影响您正在升级的现有应用程序,除非您有意修改 config/queue.php 配置文件。 And change default QUEUE_CONNECTION to database并将默认 QUEUE_CONNECTION 更改为数据库

Also change.env file (VERY IMPORTANT)还要更改.env 文件(非常重要)


10) TTL in seconds 10) TTL 秒

// Laravel 5.7 - Store item for 30 minutes...
Cache::put('foo', 'bar', 30);

// Laravel 5.8 - Store item for 30 seconds...
Cache::put('foo', 'bar', 30);


11) Carbon 2.0 Laravel now supports both Carbon 1 and Carbon 2; 11) 碳 2.0 Laravel 现在支持碳 1 和碳 2; therefore, Composer will try to upgrade to Carbon 2.0 if no other compatibility issues with any other packages are detected.因此,如果没有检测到与任何其他软件包的其他兼容性问题,Composer 将尝试升级到 Carbon 2.0。 Please review the migration guide for Carbon 2.0 .请查看Carbon 2.0 的迁移指南


12) The whereDate Method 12) whereDate 方法
The query builder's whereDate method now converts DateTime instances to Ymd format:查询生成器的 whereDate 方法现在将 DateTime 实例转换为 Ymd 格式:

// previous behaviour - SELECT * FROM `table` WHERE `created_at` > '2018-08-01 13:00:00'
$query->whereDate('created_at', '>', Carbon::parse('2018-08-01 13:00:00'));

// current behaviour - SELECT * FROM `table` WHERE `created_at` > '2018-08-01'
$query->whereDate('created_at', '>', Carbon::parse('2018-08-01 13:00:00'));

. .
. .
. .
There can be more as well.也可以有更多。 This is something I had to do when upgrading my project.这是我在升级项目时必须做的事情。 Please go through the migration guide请通过迁移指南go

At least, I hope, I was able to give you a head start.至少,我希望,我能给你一个良好的开端。 Good Luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM