在laravel中我正在尝试检查用户active
列,如果其0
必须显示为your account is disable
的简单视图,则在实现以下代码后,我会收到此错误:
我的中间件:
class CheckUserActive
{
public function handle($request, Closure $next)
{
if (auth()->check()) {
if (auth()->user()->active == 0) {
auth()->logout();
$message = 'your account is disable';
return view('layouts.frontend.pages.user-messages', compact('message'));
}
}
return $next($request);
}
}
kernel.php
:
...
protected $middlewareGroups = [
'web' => [
...
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CheckUserActive::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
...
user-messages.blade.php
:
@extends('layouts.frontend.main')
@section('content')
<div class="content-wrapper">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<p style="text-align: center;">
{!! $message !!}
</p>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
答案 0 :(得分:4)
根据文档,您可以使用
response()->view('custom');
答案 1 :(得分:1)
这是从中间件返回视图的工作
return new response(view('view.path'));
答案 2 :(得分:0)
您不应该使用中间件来返回视图,而是使用它来重定向到路由,然后从控制器返回视图。
在你的中间件中:
use Auth;
class CheckUserActive
{
public function handle($request, Closure $next)
{
if (Auth::check()) {
if (Auth::User()->active == 0) {
Auth::logout();
retrun redirect()->route('redirect-route');
}
}
return $next($request);
}
}
创建新路线:
Route::get('/', [
'as' => 'redirect-route',
'uses' => 'PageController@redirectFunction',
]);
现在在控制器中创建一个函数:
public function redirectFunction()
{
$message = 'your account is disable';
return view('layouts.frontend.pages.user-messages', compact('message'));
}
答案 3 :(得分:0)
您是否尝试删除:
$response->header->setCookie(...);
并修改:
$response->withCookie(..your cookie here...);
查看此post
答案 4 :(得分:0)
你应该试试这个:
use Auth;
class CheckUserActive
{
public function handle($request, Closure $next)
{
if (Auth::check()) {
if (Auth::user()->active == 0) {
Auth::logout();
$message = 'your account is disable';
return view('layouts.frontend.pages.user-messages', compact('message'));
}
}
return $next($request);
}
}