Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel return view on middleWare

in laravel i'm trying to check user active column and if its 0 then must be show simple view as your account is disable, after implementing below codes i get this error:

Call to a member function setCookie() on null

my middleware:

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
like image 686
DolDurma Avatar asked May 18 '18 09:05

DolDurma


People also ask

How do I return a view in Laravel?

Views may also be returned using the View facade: use Illuminate\Support\Facades\View; return View::make('greeting', ['name' => 'James']); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.

How do I load a view in Laravel?

Loading a view in the controller is easy. You just need to add `view('viewname')` method while returning from the controller method. `view()` is a global helper in Laravel. In this method, you just need to pass the name of the view.

Can Laravel use middleware?

Laravel Middleware acts as a bridge between a request and a reaction. It is a type of sifting component. Laravel incorporates a middleware that confirms whether or not the client of the application is verified. If the client is confirmed, it diverts to the home page otherwise, it diverts to the login page.


1 Answers

based on the documentation, you can access view using response()->view('custom');

like image 107
adam Avatar answered Oct 23 '22 23:10

adam