5

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 ?

Binit Ghetiya
  • 1,919
  • 2
  • 21
  • 31

1 Answers1

6

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
     }
}
Taha Azzabi
  • 2,392
  • 22
  • 31
  • _"The Illuminate\Contracts\Routing\Middleware contract has been deprecated in Laravel 5.2"_ https://stackoverflow.com/a/35487715/69537 – B Faley Nov 21 '17 at 03:50
  • i'm getting an error: PHP Fatal error: Uncaught ReflectionException: Class path.storage does not exist in /Applications/MAMP/htdocs/israplanet.com/vendor/laravel/framework/src/Illuminate/Container/Container.php:752 – aleXela Jan 22 '19 at 20:20