Middleware is one of the important part of any application. The code which we want to stick between request/response life cycle which is not necessary part of our application logic. That is called as middleware.
Let take a example to know more about middleware.
1) Adding session support
2) Parsing query strings
3) Implementing rate-limiting
4) Sniffing for bot traffic
5) Adding logging
6) Parsing request JSON
7) Anything else related to the request/response lifecycle
Laravel's Implementation Step By Step:
For implement middleware we use HttpKernelInterface. In laravel 4.x we have Foundation/Application (In built functionality). By adding this we are able to integrate middle ware in laravel.
Example:
Middleware may be specified on controller routes like so:
Route::get('profile', [
'middleware' => 'auth',
'uses' => 'UserController@showProfile'
]);
Additionally, you may specify middleware within your controller's constructor:
class UserController extends Controller {
/**
* Instantiate a new UserController instance.
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('log', ['only' => ['fooAction', 'barAction']]);
$this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
}
}
0 Comment(s)