cakephp / app

CakePHP application template
366 stars 390 forks source link

5.x: remove static method in routes.php #955

Closed LordSimal closed 11 months ago

LordSimal commented 11 months ago

This is my proposed version of how the routes.php should look like.

Why?

Depending on the application one has to build middlewares inside the routes.php Some middlewares need the application instance like our own AuthenticationMiddleware

E.g. having only some routes which need authentication and not all routes.

But accessing $this inside a static function is not allowed. Passing on $this via

return static function (RouteBuilder $routes) use ($this)

is also not possible.

So one would have to build the objects outside of the static method and pass them through, which is kind of bad imho.

If we only added that static method so IDE's can have auto-completetion for the available RouteBuilder methods this is in my opinion the better solution.

ADmad commented 11 months ago

The problem you described can be avoided by just removing the static keyword for the function, there's no need to remove the anonymous function all together.