Skip to content
On this page

Middleware

Middleware is a function that will have access for requesting an object, responding to an object, and moving to the next middleware function in the application request and response cycle.

Middleware Used

Middleware function can be used for modifying the request and response objects for tasks like adding response headers, parsing requesting bodies, and so on. Learn More

Some of the middleware used in this CMS are explained below.

language Middleware

This middleware allows the user set the desired language and access the application.

Implementation of language middleware is shown below.

public function handle($request, Closure $next)
{
    if (Cookie::get('lang') !== null) {
        $locale = Cookie::get('lang');
    } elseif (session()->get('lang') !== null) {
        $locale = session()->get('lang');
    } else {
        $locale = Config::get('constants.DEFAULT_LOCALE');
    }
    app()->setlocale($locale);
    View::share('globalLocale', $locale);

    return $next($request);
}

Authenticate Middleware

In order to check if the given user is authenticated or not and only the user who are logged in will be allowed to pass throught the other request and access the cms.

Implementation of auth middleware is shown below.

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {
        return route('login');
    }
}

frontendAuth Middleware

This middleware is used to check whether the user is authenticated or not in frontend. Only the authenticated users are allowed to perform further request in the application.

Implementation of frontendAuth middleware is shown below.

public function handle($request, Closure $next)
{
    if (! Auth::guard('frontendUsers')->check()) {
        return $this->setStatusCode(401)->userUnauthenticated();
    }
    $request = $this->addUserToRequest($request);

    return $next($request);
}