Home > Software engineering >  EasyAdmin 4 - generate URL with filter
EasyAdmin 4 - generate URL with filter

Time:04-26

I need to redirect my user to CRUD index where filter "STATUS = ACTIVE" is applied.

I've this:

$url = $this->adminUrlGenerator
            ->setController(Customer::class)

            ->generateUrl();

return $this->redirect($url);

But I can't find a way to add a filter to it. I've tried searching for something like:

->setFilter('Status', 'ACTIVE')

but without any luck. There is nothing in the docs. How to do it?

CodePudding user response:

EasyAdmin handle filters in your url by adding multiple options to handle each filters case.

  1. value to be compared with
  2. value2 (Example: between value and value2)
  3. comparison for "equal", "less than", "greater than" etc...

Filtering by Status ACTIVE would modify your url with

&filters[Status][comparison]==&filters[Status][value]=ACTIVE

Note that here = is = encoded for the url, but using = would work as well.

So when using EA AdminUrlGenerator, you can use ->set to modify options.

You would get:

$url = $this->adminUrlGenerator
            ->setController(Customer::class)
            ->set('filters[Status][value]', 'ACTIVE')
            ->set('filters[Status][comparison]', '=')
            ->generateUrl();
            

I kept the case on Status, but if your property is in lowercase, do it here as well.

  • Related