bmenant's avatar

Middleware to (pre-)parse Request parameters

I’m building an API with Lumen for which I’d like to offer a few optional parameters for my routes, such as:

filter=ofType::party|ofSize::big

(where every kind of filter is optional, and where the filter parameter itself is optional).

Basically, for each filter, I’ve created corresponding scopes in my models:

    public function scopeOfType(Builder $query, $type)
    {
        return $query->where('type', $type);
    } 
    public function scopeOfSize(Builder $query, $size)
    {
        return $query->where('size', $size);
    }

I’d like to create a FilterMiddleware to parse the Request and extract the given filters with a Collection of... filter, say. And so forth with other optional parameters like that (if any). Then, I’m not quite sure...

Should I add that Collection somewhere in the Request object to use them in my controllers’ actions?

Or should I pre-build a query somehow, in order to complete and execute that query from my controllers’ actions?

Moreover, I’m wondering if there’re a common pattern to achieve this... if it’s a proper way to use a middleware?

Advice would be greatly appreciated! :-)

0 likes
0 replies

Please or to participate in this conversation.