Easy Admin is Not Enabled Turn It Off

Dashboards

Edit this page
  • Dashboard Route
  • Dashboard Configuration
  • Customizing the Dashboard Contents
  • Main Menu
    • Menu Item Configuration Options
    • Menu Item Types
    • Submenus
    • Complex Main Menus
  • User Menu
  • Admin Context
  • Translation
  • Page Templates
    • Login Form Template
    • Content Page Template

Dashboards

Dashboards are the entry point of backends and they link to one or more resources. Dashboards also display a main menu to navigate the resources and the information of the logged in user.

Imagine that you have a simple application with three Doctrine entities: users, blog posts and categories. Your own employees can create and edit any of them but external collaborators can only create blog posts.

You can implement this in EasyAdmin as follows:

  • Create three CRUD controllers (e.g. UserCrudController, BlogPostCrudController and CategoryCrudController);
  • Create a dashboard for your employees (e.g. DashboardController) and link to the three resources;
  • Create a dashboard for your external collaborators (e.g. ExternalDashboardController) and link only to the BlogPostCrudController resource.

Technically, dashboards are regular Symfony controllers so you can do anything you usually do in a controller, such as injecting services and using shortcuts like $this->render() or $this->isGranted().

Dashboard controller classes must implement the EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface, which ensures that certain methods are defined in the dashboard. Instead of implementing the interface, you can also extend from the AbstractDashboardController class. Run the following command to quickly generate a dashboard controller:

If you now visit the /admin URL of your application, you'll see the default EasyAdmin Welcome Page:

EasyAdmin 4 Welcome Page

Later in this article you'll learn how to customize that page. If you don't see the Welcome Page, you might need to configure the URL of your backend as explained in the next section.

Dashboard Route

Each dashboard uses a single Symfony route to serve all its URLs. The needed information is passed using query string parameters. If you generated the dashboard with the make:admin:dashboard command, the route is defined using Symfony route annotations or PHP attributes (if the project requires PHP 8 or newer).

The only requirement is to define the route in a controller method named index(), which is the one called by EasyAdmin to render the dashboard:

  • Annotations
  • Attributes

Note

Since index() is part of the Dashboard interface, you cannot add arguments to it to inject dependencies. Instead, inject those dependencies in the constructor method of the controller.

The /admin URL is only a default value, so you can change it. If you do that, don't forget to also update this value in your Symfony security config to restrict access to the entire backend.

There's no need to define an explicit name for this route. Symfony autogenerates a route name and EasyAdmin gets that value at runtime to generate all URLs. However, if you generate URLs pointing to the dashboard in other parts of your application, you can define an explicit route name to simplify your code:

  • Annotations
  • Attributes

If you don't use annotations, you must configure the dashboard route using YAML, XML or PHP config in a separate file:

  • YAML
  • XML
  • PHP

In practice you won't have to deal with this route or the query string parameters in your application because EasyAdmin provides a service to generate admin URLs.

Note

Using a single route to handle all backend URLs means that generated URLs are a bit long and ugly. This is a reasonable trade-off because it makes many other features, such as generating admin URLs, much simpler.

Dashboard Configuration

The dashboard configuration is defined in the configureDashboard() method (the main menu and the user menu are configured in their own methods, as explained later):

4.1.0

The disableUrlSignatures() dashboard method was deprecated in EasyAdmin 4.1.0 because backend URLs no longer include signatures.

Customizing the Dashboard Contents

Generated dashboards display by default a "Welcome Page" with some useful links. In a real application you'll need to customize this page to display your own contents.

Dashboards usually display widgets and charts with stats. EasyAdmin doesn't provide yet any way of creating those widgets. It's in our list of future features, but meanwhile you can use Symfony UX Chart.js bundle to create those charts and render them in your own Twig template:

Another popular option is to make the dashboard redirect to the most common task for people working on the backend. This requires generating admin URLs, and CRUD controllers, which is explained in detail later:

The main menu links to different CRUD controllers from the dashboard. It's the only way to associate dashboards and resources. For security reasons, a backend can only access to the resources associated to the dashboard via the main menu.

The main menu is a collection of objects implementing EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuInterface that configure the look and behavior of each menu item:

The first argument of MenuItem::new() is the label displayed by the item and the second argument is the full CSS class of the FontAwesome icon to display.

All menu items define the following methods to configure some options:

  • setCssClass(string $cssClass), sets the CSS class or classes applied to the <li> parent element of the menu item;
  • setLinkRel(string $rel), sets the rel HTML attribute of the menu item link (check out the allowed values for the "rel" attribute);
  • setLinkTarget(string $target), sets the target HTML attribute of the menu item link (_self by default);
  • setPermission(string $permission), sets the Symfony security permission that the user must have to see this menu item. Read the menu security reference for more details.
  • setBadge($content, string $style='secondary'), renders the given content as a badge of the menu item. It's commonly used to show notification counts. The first argument can be any value that can be converted to a string in a Twig template (numbers, strings, stringable objects, etc.) The second argument is one of the predefined Bootstrap styles (primary, secondary, success, danger, warning, info, light, dark) or an arbitrary string content which is passed as the value of the style attribute of the HTML element associated to the badge.

The rest of options depend on each menu item type, as explained in the next sections.

This is the most common menu item type and it links to some action of some CRUD controller. Instead of passing the FQCN (fully-qualified class name) of the CRUD controller, you must pass the FQCN of the Doctrine entity associated to the CRUD controller:

It links to the homepage of the current dashboard. You can achieve the same with a "route menu item" (explained below) but this one is simpler because you don't have to specify the route name (it's found automatically):

It links to any of the routes defined by your Symfony application:

It links to a relative or absolute URL:

To avoid leaking internal backend information to external websites, EasyAdmin adds the rel="noopener" attribute to all URL menu items, except if the menu item defines its own rel option.

It creates a visual separation between menu items and can optionally display a label which acts as the title of the menu items below:

It links to the URL that the user must visit to log out from the application. If you know the logout route name, you can achieve the same with the "route menu item", but this one is more convenient because it finds the logout URL for the current security firewall automatically:

Note

The logout menu item will not work under certain authentication schemes like HTTP Basic because they do not have a default logout path configured due to the nature of how those authentication schemes work.

If you encounter an error like "Unable to find the current firewall LogoutListener, please provide the provider key manually.", you'll need to remove the logout menu item or add a logout provider to your authentication scheme.

It links to the URL that the user must visit to stop impersonating other users:

The main menu can display up to two level nested menus. Submenus are defined using the subMenu() item type:

Note

In a submenu, the parent menu item cannot link to any resource, route or URL; it can only expand/collapse the submenu items.

Complex Main Menus

The return type of the configureMenuItems() is iterable, so you don't have to always return an array. For example, if your main menu requires complex logic to decide which items to display for each user, it's more convenient to use a generator to return the menu items:

When accessing a protected backend, EasyAdmin displays the details of the user who is logged in the application and a menu with some options like "logout" (if Symfony's logout feature is enabled).

The user name is the result of calling to the __toString() method on the current user object. The user avatar is a generic avatar icon. Use the configureUserMenu() method to configure the features and items of this menu:

Admin Context

EasyAdmin initializes a variable of type EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext automatically on each backend request. This object implements the context object design pattern and stores all the information commonly needed in different parts of the backend.

This context object is automatically injected in every template as a variable called ea (the initials of "EasyAdmin"):

The AdminContext variable is created dynamically on each request, so you can't inject it directly in your services. Instead, use the AdminContextProvider service to get the context variable:

In EasyAdmin's CRUD controllers and in Symfony controllers integrated into EasyAdmin, use the AdminContext type-hint in any argument where you want to inject the context object:

Translation

The backend interface is fully translated using the Symfony translation features. EasyAdmin own messages and contents use the EasyAdminBundle translation domain (thanks to our community for kindly providing translations in tens of languages).

The rest of the contents (e.g. the label of the menu items, entity and field names, etc.) use the messages translation domain by default. You can change this value with the translationDomain() method:

Internally, EasyAdmin manages translations via TranslatableMessage objects. These objects are passed to the templates, where they are translated into the user locale. You can also use TranslatableMessage objects to define any text content in your backends (e.g. the label of some field, the help contents of some page, etc.):

Tip

Using translatable objects is recommended for multilingual backends because Symfony can extract all of them automatically to update your translation files.

The backend uses the same language configured in the Symfony application. When the locale is Arabic (ar), Persian (fa) or Hebrew (he), the HTML text direction is set to rtl (right-to-left) automatically. Otherwise, the text is displayed as ltr (left-to-right), but you can configure this value explicitly:

Tip

If you want to make the backend use a different language than the public website, you'll need to work with the user locale to set the request locale before the translation service retrieves it.

Note

The contents stored in the database (e.g. the content of a blog post or the name of a product) are not translated. EasyAdmin does not support the translation of the entity property contents into different languages.

Page Templates

EasyAdmin provides several page templates which are useful when adding custom logic in your dashboards.

Login Form Template

Twig Template Path: @EasyAdmin/page/login.html.twig

It displays a simple username + password login form that matches the style of the rest of the backend. The template defines lots of config options, but most applications can rely on its default values:

Content Page Template

Twig Template Path: @EasyAdmin/page/content.html.twig

It displays a simple page similar to the index/detail/form pages, with the main header, the sidebar menu and the central content section. The only difference is that the content section is completely empty, so it's useful to display your own contents and custom forms, to integrate Symfony actions inside EasyAdmin, etc. Example:

meyersriduch.blogspot.com

Source: https://symfony.com/bundles/EasyAdminBundle/current/dashboards.html

0 Response to "Easy Admin is Not Enabled Turn It Off"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel