New in EasyAdmin: Pretty URLs

Javier Eguiluz - Nov 3 - - Dev Community

The most requested feature for EasyAdmin has just arrived in version 4.14.0.

Since day one, EasyAdmin has used query string parameters to pass the necessary
information for rendering backend pages. This approach created URLs like:

https://example.com/admin?crudAction=edit&crudControllerFqcn=App%5CController%5CAdmin%5CPostCrudController&entityId=3874
Enter fullscreen mode Exit fullscreen mode

While this URL format is ugly, it's functional. Some browsers, like Safari, even
hide most of the URL by default, showing only the hostname, so the impact on
end-users is minimal.

However, starting today, EasyAdmin generates cleaner URLs that look like this:

https://example.com/admin/post/3874/edit
Enter fullscreen mode Exit fullscreen mode

In addition to being the most requested feature, we implemented this change for
two main reasons:

  • The old, ugly URL leaks internal details about the application, like the namespace of the CRUD controller. This is typically fine, as most backends aren't sharing URLs with external users, but it's not ideal.
  • Building these ugly URLs with the AdminUrlGenerator class is cumbersome. Now, you can use route names like admin_post_edit or admin_user_index and rely on Symfony's built-in features to generate the URLs.

How to Enable Pretty URLs?

This feature has been designed to work seamlessly with current applications. If
you don't make any changes, the application will still use ugly URLs, and
everything will continue to work as before.

If you'd like to start using pretty URLs now, create the following configuration
file in your application:

# config/routes/easyadmin.yaml
easyadmin:
    resource: .
    type: easyadmin.routes
Enter fullscreen mode Exit fullscreen mode

This configuration enables a custom Symfony route loader, a class that
automatically generates routes in your application. Clear the cache (by running
php bin/console cache:clear or rm -fr var/cache/*) and the application will
begin using pretty URLs. Run the following command to see all the newly generated
routes:

php bin/console debug:router
Enter fullscreen mode Exit fullscreen mode

You can now also update your code to stop building backend URLs with the
AdminUrlGenerator utility and use the new route names directly:

// Before:
$this->adminUrlGenerator()
    // this is only needed if you have ore than 1 dashboard
    ->setDashboard(SomeDashboardController::class)
    ->setController(ProductCrudController::class)
    ->setAction('detail')
    ->setEntityId(321)
    ->generateUrl();

// After:
$this->router->generate('admin_product_detail', ['entityId' => 321]);
Enter fullscreen mode Exit fullscreen mode

The generated routes are composed of three parts: dashboard + crud + action. Each
of these can be customized using the new PHP attributes #[AdminDashboard],
#[AdminCrud], and #[AdminAction]. Read the docs to learn more.

Pretty URLs are still optional in EasyAdmin 4.x versions, but ugly URLs are now
deprecated. Starting in EasyAdmin 5.x (planned for release soon), pretty URLs
will be required.


✨ If you enjoyed this feature and want to see more like it, consider
sponsoring the EasyAdmin project 🙌💡

. .
Terabox Video Player