We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Best way to impliment dynamic controllers for an API in Micro()

Does anyone have any pointers on ways to implement the following: I'm looking for an efficient way to map multiple routes to two main controllers while still being able to get part of that route from the matched controller. Example: /v1/api/[type-of-record] would get matched to an ApiController and Get action. That's fine, but I need to be able to get the type-of-record while in that action. I've tried setting the name on each route as part of a micro collection (as the third parameter - but that only works for GET bindings).

The type-of-record is set by the database so I cannot create a controller for each. The best way I've found is use a catch-all e.g. /v1/api/{record-type:[a-zA-Z\-]+} and then using the before event to 404 or redirect if that record type isn't allowed.

There must be a better way to do this though. Any thoughts?



43.9k

Hi,

what is [type-of-record], a model ?

for accessing type-of-record in your getAction:

    public function getAction($type-of-record){ ...}    


515

type-of-record is a model (the same model for all types - which are baiscally validated json structures against a shema). I can get the [type-of-record] fine from the action, but only when im using the preg matched routing. I was hoping to absolutely define each route and somehow name the micro collection.