Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

canceling route execution with micro's before middleware

Hi,

I want to use middleware to halt execution of a route.

/* 1. This will halt execution as expected. */

$app->before(function() use ($app) { return false; });

/* 2. This does not halt execution. */

class Middleware implements \Phalcon\Mvc\Micro\MiddlewareInterface

{
    public function call($app)
    {
        return false;
    }
}


$app->before(new Middleware());

/* 3. This does. */

class Middleware implements \Phalcon\Mvc\Micro\MiddlewareInterface

{
    public function call($app)
    {
        return false;
    }
}


$app->before(function() use ($app) {
    $middleware = new Middleware();
    return $middleware->call($app);
});

Is this a bug? I can use the workaround in #3, but why doesn't #2 work?

phalcon 1.3.4

Thanks!

Has no one else run into this?

In either case, does anyone foresee any problems using the working in #3?

Thanks for the help.



392

I've just stumbled across this too. I'm using Phalcon 2. The documention does make out that #2 should work. #3 is the only way I can think to get round it. Let me know if you find out any more about it!

Good to know it's still happening in v2.



6.7k
Accepted
answer

Take a look at this

edited Apr '16

Well, it does initiate call() method if you use this syntax:

$app->before(new MyMiddleware());

It does bind your Micro $app by default (lazy loading?). It's just that it does not care about return value, i.e. will not stop execution if you return false from your middleware.

So, this is a buggy bug.

Here's it: https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/micro.zep#L659

Line 659: https://api.phalconphp.com/source/Phalcon/Mvc/Micro.html

This is still unresolved in 3.0.x version.