Question: How does check of authentication work in laravel?


How does check of authentication work in laravel?

Answers 4
Added at 2017-01-01 13:01

I have the following line of code in my controller:

public function __construct() {

So basically what this line of code does is it checks before any method in the controller is run that the user is authenticated , I have a little confusion about this line of code though , what i want to know is where is this property middleware coming form ?? and how does this this method middleware really get included into my controller ??

Thank you.

Answers to

How does check of authentication work in laravel?

nr: #1 dodano: 2017-01-01 13:01

Middlewares are listed inside App/Http/Kernel.php file. From there you can see that, auth uses the \App\Http\Middleware\Authenticate class which checks the request.

nr: #2 dodano: 2017-01-01 13:01

This line applies middleware to all controller methods. This particular middleware checks if user is authenticated or not. If user is not authenticated, middleware redirects him to some defined page.

Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

You can learn more about middleware in docs.

nr: #3 dodano: 2017-01-01 13:01

YourController extends Controller class

class YourController extends Controller {  }

If you navigate to Controller class you can find middleware method and property.


protected $middleware = [];

public function middleware($middleware, array $options = [])
    foreach ((array) $middleware as $m) {
        $this->middleware[] = [
            'middleware' => $m,
            'options' => &$options,
nr: #4 dodano: 2017-01-01 13:01

Laravel App\Http\Controllers\Controller extends Illuminate\Routing\Controller class which contains the middleware method and property.

Source Show
◀ Wstecz