Why do Middleware implements no interface?

Viewed 321

In Laravel < 5.2, middlewares implemented an interface described by this contract: Illuminate\Contracts\Routing\Middleware.

Now, a Middleware created with artisan does not implement anything anymore. And the contract was removed with no explaination (unless I mistake).

Is there a reason?

2 Answers

As per laravel 5.2 upgrade guide at official documentation under Deprecations section:

The Illuminate\Contracts\Routing\Middleware contract has been deprecated. No contract is required on your middleware. In addition, the TerminableMiddleware contract has also been deprecated. Instead of implementing the interface, simply define a terminate method on your middleware.

I also noticed that middleware did not use an interface in other frameworks such as Slim and I read that Relay also does not use an interface.

The answer that the Relay people gave for their case was since a lot of middleware accepts closures (i.e. functions instead of classes) which generally can't be type hinted from php's perspective, forcing the use of an interface would not work. And if there isn't any forcing the implementation of an interface, then there is no point of using one.

Related