Slim PHP: Only catch valid routes with middleware

前端 未结 3 874
遥遥无期
遥遥无期 2020-12-20 17:51

I\'m writing a REST API with Slim. I have written a small middleware to protect the resources so only authenticated users will be able to access them:



        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 18:52

    I use a hook to do what you're trying to do, as MamaWalter suggested, but you want to use slim.before.dispatch rather than an earlier hook. If the route your user is trying to visit doesn't exist, the hook will never be called and the 404 gets thrown.

    I'm doing exactly that in my own Authorization Middleware. Works like a charm.

提交回复
热议问题