Binit Ghetiya
Binit Ghetiya

Reputation: 1979

how to create middleware in octobercms

I'm new to OctoberCMS but I have moderate knowledge about Laravel.

In Laravel it is easy to create middleware and group more than one middleware.

In OctoberCMS I can't find proper guidelines or a satisfactory answer yet.

Does anyone know how to create middleware and group more then one middleware in OctoberCMS ?

Upvotes: 5

Views: 3223

Answers (1)

Taha Azzabi
Taha Azzabi

Reputation: 2570

In your plugin folder, use the file Plugin.php to set up your middleware You must declare in boot function like this :

  public function boot()
  {
    // Register middleware
     $this->app['Illuminate\Contracts\Http\Kernel']
          ->pushMiddleware('Experty\Experts\Middleware\ExpertsMiddleware');
  }

and in ExpertsMiddleware.php

<?php namespace Experty\Experts\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Http\Response;
use October\Rain\Exception\AjaxException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExpertsMiddleware implements Middleware
{
/**
     * The Laravel Application
     *
     * @var Application
     */
    protected $app;

    /**
     * Create a new middleware instance.
     *
     * @param  Application $app
     * @return void
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
    }
 /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */

    public function handle($request, Closure $next)
    {
      //youre code
     }
}

Upvotes: 6

Related Questions