laravel由5.4版本直接升级7.x版本笔记

记一次laravel5.4直接升级7.x笔记

由于我们公司使用框架一直是laravel5.4,所以最近学习这个框架,框架优点我就不多说了,最近发现7.x版本比5.4好,所以干脆试着直接尝试升级。

所谓直接升级,其实是生成一个新项目,再安装相关插件,然后复制原项目的路由、模块、控制器还有页面,最后调整相关配置文件。这样操作有些粗暴,容易产生一些问题,但没关系,尽量尝试自己解决。以防忘记,所以记下升级过程所遇到的一些问题。

1. 多用户认证重定向未认证用户

只要涉及多用户认证,就必定会涉及重定向问题,每种用户验证失效后需要重定向到不同的登录界面。

5.4版本时问题已解决
重写Handler.php中的unauthenticated方法:

protected function unauthenticated($request, AuthenticationException $exception)
{
  if ($request->expectsJson()) {
     return response()->json(['error' => 'Unauthenticated.'], 401);
     }  switch ($exception->guards()[0]){
         case 'admin':
              return redirect()->guest(route('admin_login'));
         case 'source':
             return redirect()->guest(route('source_login'));
        case 'users':
             return redirect()->guest(route('manager_login'));
         case 'test':
             return redirect()->guest(route('test_login'));
         case 'inspectoral':
              return redirect()->guest(route('ins_login'));
        default:
             return redirect()->guest(route('admin_login'));
     }
 }

升级7.x之后,方便很多,有了更直接的方法

laravel由5.4版本直接升级7.x版本笔记

但是这个不适用于多用户认证,所以这方面我直接还原回5.4版本
需要修改app/Http/Kernel.php和\Illuminate\Auth\Middleware\Authenticate::class
app/Http/Kernel.php中

laravel由5.4版本直接升级7.x版本笔记
改回

'auth' => \Illuminate\Auth\Middleware\Authenticate::class,

\Illuminate\Auth\Middleware\Authenticate::class中修改成

 /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string[]  ...$guards
     * @return mixed
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    public function handle($request, Closure $next, ...$guards)
    {
        $this->authenticate($request, $guards);

        return $next($request);
    }

    /**
     * Determine if the user is logged in to any of the given guards.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  array  $guards
     * @return void
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    protected function authenticate($request, array $guards)
    {
        if (empty($guards)) {
            $guards = [null];
        }

        foreach ($guards as $guard) {
            if ($this->auth->guard($guard)->check()) {
                return $this->auth->shouldUse($guard);
            }
        }

        $this->unauthenticated($request, $guards);
    }

    /**
     * Handle an unauthenticated user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  array  $guards
     * @return void
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    protected function unauthenticated($request, array $guards)
    {
        throw new AuthenticationException(
            'Unauthenticated.', $guards
        );
    }

这样就回到5.4版本了

2. CSRF 保护问题

很显然,在7.x版本中 CSRF 保护更彻底以及完善,升级7.x之后,原来的一些请求就会报错,所以要特别注意X-CSRF-TOKEN。

laravel由5.4版本直接升级7.x版本笔记

3.文件上传路径修改

为方便直接访问上传文件,在5.4版本时就把上传文件文件夹修改到public中,7.x中这方面会有点不同。

需要修改filesystems 配置文件

   'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => public_path('storage'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => public_path('storage'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ]

    ],

4.集合使用问题

在使用laravel框架时就发现数据查询和集合时可以结合一起使用,所以之前就偷懒放一起了,导致数据导致我的数据查询豫剧看起来比较复杂以及臃肿:joy:

比如:

 $data = DB::table('apply_main')
            ->join('manager_corp3', 'apply_main.rzwtid', '=', 'manager_corp3.id')//Inner Join 语句
            ->join('apply_sp', 'apply_main.spid', '=', 'apply_sp.id')//Inner Join 语句
            ->orderBy('apply_main.created_at', 'desc')//排序
            ->where('apply_main.issubmit', 1)//简单的 Where 语句
            ->whereIn('apply_sp.type', [14,16])//whereIn
            ->select( 'manager_corp3.rzwt','manager_corp3.zcadd','apply_main.spid','apply_main.applyname','apply_main.attestation','apply_sp.type','apply_main.created_at','apply_sp.allid','apply_sp.other->zuzhang as zuzhang','apply_sp.other->appids as appids')    
            ->groupby('spid')//去重
            ->get();

但是升级到7.x之后groupby()就报错了:sob:,找了半天发现在7.x中去重用unique(),还得放到get()后面。其实我5.4的时候尝试过用unique(),但是也报错,这也是我要升级到7.x的一个原因吧!

修改完以上问题后,系统勉强能用,也许还会出现问题,但我们会尽快解决,也会尽量记下来。

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 5
洛未必达

系统业务复杂度如何?真的猛士,敢于直接升级 :joy:

3年前 评论
seeker-x2y 2年前
semlie1994 (楼主) 3年前

真猛,这么大跨度升级

3年前 评论

够狠 :+1: :joy: :joy:

3年前 评论

额,,前几天把我博客,从5.8升级到7.22.x,,,因为我写了单元测试,,,所以,,,毫无压力啦,,,

3年前 评论
semlie1994 (楼主) 3年前
largezhou (作者) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!