專案 Laravel 框架 5.1 升級到 5.5 過程中的一些問題

wangjin112發表於2019-10-11

[TOC]

Composer映象加速:

   $> composer config -g repo.packagist composer https://mirrors.aliyun.com/composer

版本之間檔案修改對比檢視地址:

https://github.com/laravel/laravel/compare...

以下只列出部分修改,其他修改請參照各版本的升級指南

Laravel5.1升級到5.2

1、更新依賴

刪除illuminate/htmllaravelcollective/html包,使用composer remove 包名命令

更新composer.json

Require部分laravel/framework 5.2.*專案所有依賴檢查是否支援5.2框架版本並更新

Require-dev部分新增symfony/dom-crawler ~3.0symfony/css-selector ~3.0

執行

   $> composer update -vvv

如果出現錯誤Something's changed, looking at all rules again (pass #23),說明專案依賴包與框架包不匹配

2、config調整

app.php檔案中,刪除

Illuminate\Foundation\Providers\ArtisanServiceProvider

Illuminate\Routing\ControllerServiceProvider

Illuminate\Html\HtmlServiceProvider

App\Providers\BusServiceProvider

3、Controller.php基類

use Illuminate\Foundation\Bus\DispatchesCommands

更換為

use Illuminate\Foundation\Bus\DispatchesJobs;

use DispatchesCommands

更換為

use DispatchesJobs

4、.env配置檔案變數更新

APP_DOMAIN_ROOT=laraveltest.com
APP_DOMAIN_ADMIN=admin.{$APP_DOMAIN_ROOT}

{$PARAM}變數為${PARAM}

5、Jobs調整

在建立任務/命令時你不再需要實現SelfHandling契約

6、database.php中connections.mysql.strict

strictfalse

5.1版本中
set session sql_mode=''

5.2及之後版本
set session sql_mode='NO_ENGINE_SUBSTITUTION'

可通過配置項modes進行相容
sql_modes參考地址https://www.cnblogs.com/Zender/p/8270833.h...

Laravel5.2升級到5.3

1、更新依賴

更新composer.json

Require部分laravel/framework 5.3.*專案所有依賴檢查是否支援5.2框架版本並更新

Require-dev部分phpunit/phpunit ~5.0symfony/dom-crawler 3.1.*symfony/css-selector 3.1.*

執行

   $> composer update -vvv

如果出現錯誤Something's changed, looking at all rules again (pass #23),說明專案依賴包與框架包不匹配

laravel/tinker安裝

2、Providers檔案修改

EventServiceProviders.php

public function boot(DispatcherContract $events)
    {
        parent::boot($events); 

改為

public function boot()
    {
    parent::boot();

RouteServiceProvider.php

public function boot(Router $router)
{
    //
    parent::boot($router);

改為

public function boot()
{
    //
    parent::boot(); 

3、scode錯誤

Interface 'Illuminate\Database\Eloquent\ScopeInterface' not found

使用到的地方調整為Illuminate\Database\Eloquent\Scope

4、session

5.3及以後的版本controller的構造方法____construct無法獲取session,
原因5.3之前版本路由中介軟體先執行再例項控制器

Illuminate\Routing\Router.php

    protected function runRouteWithinStack(Route $route, Request $request)
    {
        $shouldSkipMiddleware = $this->container->bound('middleware.disable') &&
                                $this->container->make('middleware.disable') === true;

        $middleware = $shouldSkipMiddleware ? [] : $this->gatherRouteMiddlewares($route);

        return (new Pipeline($this->container))
                        ->send($request)
                        ->through($middleware)
                        ->then(function ($request) use ($route) {
                            return $this->prepareResponse(
                                $request,
                                $route->run($request)//此處進行控制器例項化
                            );
                        });
    } 

5.3及以後版本中獲取控制器路由中介軟體時會先例項化控制器再執行路由中介軟體
Illuminate\Routing\Router.php

    protected function runRouteWithinStack(Route $route, Request $request)
    {
        $shouldSkipMiddleware = $this->container->bound('middleware.disable') &&
                                $this->container->make('middleware.disable') === true;

        $middleware = $shouldSkipMiddleware ? [] : $this->gatherRouteMiddleware($route);//此處獲取路由中介軟體時例項化控制器

        return (new Pipeline($this->container))
                        ->send($request)
                        ->through($middleware)
                        ->then(function ($request) use ($route) {
                            return $this->prepareResponse(
                                $request, $route->run()
                            );
                        });
    }

可用方法如下:

    $this->middleware(function ($request, $next) {
            $passport = $request->session()->get('passport');

            return $next($request);
        }); 

或者
App\Http\Kernel$middleware屬性開啟session中介軟體StartSessionEncryptCookies

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Session\Middleware\StartSession::class,
    ]; 

Laravel5.3升級到5.4

1、更新依賴

更新composer.json

Require部分laravel/framework 5.4.*專案所有依賴檢查是否支援5.4框架版本並更新

Require-dev部分phpunit/phpunit ~5.7

執行

   $> composer update -vvv

如果出現錯誤Something's changed, looking at all rules again (pass #23),說明專案依賴包與框架包不匹配

2、Session

Session::set()改為Session::put()

$request->session()->set()改為$request->session()->put()

3、UrlGenerator

Illuminate\Routing\UrlGenerator類的forceSchema方法已經被重新命名為forceScheme

4、Model

5.4版本開始獲取屬性時,會呼叫
Illuminate\Routing\Database\Eloquent\Concerns\HasAttributes.php
getAttributeValue方法,自增主鍵及$casts屬性中設定的型別會進行強制轉換

作用域Illuminate\Routing\Database\Eloquent\Concerns\HasGlobalScopes.php

新增事件Illuminate\Routing\Database\Eloquent\Concerns\HasEvents.php

5、pagination

Illuminate\Pagination\AbstractPaginator.php檔案中

addQuery方法修飾由public改為protected

直接呼叫addQuery方法的地方可改為呼叫appends方法

6、中介軟體

app/Http/Kernel.phpmiddleware新增中介軟體\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class
將空字串轉換為null,當mysql配置嚴格模式strict設定為false時,新增資料時空欄位的值null,導致執行sql語句報錯,可取消此中介軟體

Laravel5.4升級到5.5

1、更新依賴

更新composer.json

Require部分php >=7.0.0,laravel/framework 5.5.*,fideloper/proxy:~3.3專案所有依賴檢查是否支援5.5框架版本並更新

Require-dev部分phpunit/phpunit ~6.0 ,filp/whoops ~2.0

執行

   $> composer update -vvv

如果出現錯誤Something's changed, looking at all rules again (pass #23),說明專案依賴包與框架包不匹配

2、Config

app.php刪除'Laravel\Tinker\TinkerServiceProvider',

3、index.php

public/index.php

require __DIR__.'/../bootstrap/autoload.php';

改為

require __DIR__.'/../vendor/autoload.php'; 

4、cookie中介軟體EncryptCookies

5.4版本及5.5低版中,cookie加密時預設serialize序列化,解密時預設unserialize反序列化。5.5高版中cookie加解密預設不進行序列化。

Illuminate\cookie\Middleware\EncryptCookies.php

低版本中

protected function encrypt(Response $response)
{
        foreach ($response->headers->getCookies() as $cookie) {
            if ($this->isDisabled($cookie->getName())) {
                continue;
            }

            $response->headers->setCookie($this->duplicate(
                $cookie, $this->encrypter->encrypt($cookie->getValue())
            ));
        }

        return $response;
 }

高版本中

protected function encrypt(Response $response)
{
        foreach ($response->headers->getCookies() as $cookie) {
            if ($this->isDisabled($cookie->getName())) {
                continue;
            }

            $response->headers->setCookie($this->duplicate(
                $cookie, $this->encrypter->encrypt($cookie->getValue(), static::serialized($cookie->getName()))
            ));
        }

        return $response;
}

$this->encrypterIlluminate\Encryption\Encrypter.php
方法encrypt($value, $serialize = true)

多個系統利用redis進行共享session時,如果版本不一致導致cookie加解密失敗,
如果希望跟之前保持一致可修改app\Http\Middelware\EncryptCookies.php檔案將父類Illuminate\cookie\Middleware\EncryptCookies$serialize屬性設定為true

class EncryptCookies extends Middleware
{

    protected static $serialize = true;

    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}

5、request的only方法

5.4及之前版本,如果對應鍵值不存在,對應鍵值為null
5.5版本,如果對應鍵值不存在,對應鍵不返回

相關文章