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之后,方便很多,有了更直接的方法
但是这个不适用于多用户认证,所以这方面我直接还原回5.4版本
需要修改app/Http/Kernel.php和\Illuminate\Auth\Middleware\Authenticate::class
app/Http/Kernel.php中
改回
'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。
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框架时就发现数据查询和集合时可以结合一起使用,所以之前就偷懒放一起了,导致数据导致我的数据查询豫剧看起来比较复杂以及臃肿
比如:
$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()就报错了,找了半天发现在7.x中去重用unique(),还得放到get()后面。其实我5.4的时候尝试过用unique(),但是也报错,这也是我要升级到7.x的一个原因吧!
修改完以上问题后,系统勉强能用,也许还会出现问题,但我们会尽快解决,也会尽量记下来。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: