Middleware applied to Route Group does not work


#1

Hi guys,
I’m having this problem.

I have a Middleware who take care to handle script mocking env as usually done to hande bash script with slim.

My idea was to add this middleware ONLY to those routes related to crontab, but, apparently, they work only if are applied to application directly.
Let me explain:

My middleware looks so:

class Cli extends AbstractMiddleware
{
	public function __invoke($request, $response, $next)
	{

		global $argv;
		if (isset($argv)) {
			@list($call, $path, $params) = $argv;
			$request = Request::createFromEnvironment(Environment::mock([
				'REQUEST_METHOD' => 'GET',
				'REQUEST_URI'    => $path . '?' . $params,
				'QUERY_STRING'   => $params
			]));
			unset($argv);
		}
		return $next($request, $response);
	}

}

This middleware should be applied to crontab route group:

  $this->app->group('/crontab/', function() {
        //Namespace for Group
        $this->get('test', Test::class)
	        ->setName('crontab-test');

    })->add(Cli::class);

But in these way, it does not work, I get a 404 not found error. Any Idea?

thanks in advance


#2

Should this

$this->get('test', Test::class)

Be instead:

$this->app->get('test', Test::class)

#3

Hi man,

it’s a transcription error; that route is within a routegroup, as follow:

public function crontabRouting()
{

    $this->app->group('/crontab/', function() {
        /**
         * Crontab
         */
    	$this->group('partner/', function(){
	        $this->get('import-category', ImportCategory::class);
        });
    }) ->add(Cli::class);  <<< this middleware does not work!!!
}

#4

Hi,

I call with new in my routes.php file:

$this->app->group('/crontab/', function() {
    //Namespace for Group
    $this->get('test', Test::class)
        ->setName('crontab-test');

})->add(new Cli());

maybe could this!