phpmiddlewarefat-free-framework

How do I create middleware specific to route not whole controller class


I have an api and some routes are public some need to be protected via auth. I want to have them in one controller class as they are related. I can extend the controller and have beforeRoute function but it runs for any route that is in that controller. is it possible to add a middleware only to specific routes? I'm a js dev and in express I can just pass middleware functions for any route, even multiple middlewares.

class Clanky /*extends \controllers\ProtectedController */{
    
    public function post_novy_clanek(\Base $base) {
//needs to be protected
    }

    public function get_clanky(\Base $base) {

    }


    public function get_clanek(\base $base) {

    }

    public function get_kategorie(\Base $base) {

    }
}

PHP is new to me, I just want to know how I can implement the concepts I know from other languages and frameworks in this weird fatfree framework. Thanks.


Solution

  • Use can use f3-access plugin for that purpose https://github.com/xfra35/f3-access Fatfree is not opinionated about how to do this.. other options to solve this ask might be: