Easyadminbundle: [3.0.0-dev] Variable "ea" does not exist.

Created on 29 Apr 2020  路  29Comments  路  Source: EasyCorp/EasyAdminBundle

Variable "ea" does not exist.
I'm following ,
https://symfony.com/doc/master/bundles/EasyAdminBundle/index.html
UPGRADED FROM 2 TO 3, by following: https://symfony.com/doc/master/bundles/EasyAdminBundle/upgrade.html

And created my dashboard using https://symfony.com/doc/master/bundles/EasyAdminBundle/dashboards.html
at https://symfony.com/doc/master/bundles/EasyAdminBundle/dashboards.html#dashboard-route

This is my first run after upgrading to 3.0. Throws error:
Variable "ea" does not exist.

and debug toolbar error log is:
| Uncaught PHP Exception Twig\Error\RuntimeError: "Variable "ea" does not exist." at /project/vendor/easycorp/easyadmin-bundle/src/Resources/views/layout.html.twig line 2 { "exception": {} } -- | --
At line 2 in layout.html.twig:
{% trans_default_domain ea.i18n.translationDomain %}

composer:
"easycorp/easyadmin-bundle": "3.0.x-dev"

I don't have any yaml config files.

Most helpful comment

Solution: Update Symfony to 5.1 so its works by me.

All 29 comments

I also tried v3.0.0-alpha7,v3.0.0-alpha6,v3.0.0-alpha5 : same error.
INFO: To run old version I added symfony uid to my project. - So the project works, but failed while accesing admin.

same error problem {% for css_asset in ea.assets.cssFiles ?? [] %} in layout.html-twig

same error problem {% for css_asset in ea.assets.cssFiles ?? [] %} in layout.html-twig

Yes. The template doesn't know what is ea is.

As per doc: https://symfony.com/doc/master/bundles/EasyAdminBundle/dashboards.html#admin-context
I'm new to this area of knowledge in symfony. Here in this doc they mentioned This context object is automatically injected in every template as a variable called ea

But this is not working...

I tried to inject ea in template as per service inject in twig.
Doc: https://symfony.com/doc/master/bundles/EasyAdminBundle/dashboards.html#admin-context

  1. Created a new service (ref to SomeService)
  2. Called in twig as,
twig:
    globals:
        ex:                     '@App\Service\EAContext'

and SET eg = ex in twig.
SAME ERROR , IT'S NOT SOLVING

I just migrated my old Easyadmin 2.3.7 to 3.x alpha 7 with latest Symfony 5.0.8 and the same error

image

@javiereguiluz Please look at this issue. We need your help on this. Or guide me how to solve this.

I think the problem ist ->addGlobal dosn't work corect. Next problem in ContexProvider not full methods existing now. Waiting for alpha8

Got the same problem with a simple native Symfony AbstractController that renders a template which extends @EasyAdmin/layout.html.twig.
The ea twig global injection and other magic happens in the AdminContextListener. First check there is if the controller is a EasyAdmin controller (DashboardControllerInterface).

Extending _AbstractDashboardController_ instead of _AbstractController_ fixed it for me. No problem for that part of my application so far. You can also just implement DashboardControllerInterfaceand add the required methods. I added an example ... did it anyway.

use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

final class IndexController extends AbstractDashboardController
{
    /**
     * @Route("/", name="app", methods={"GET"})
     */
    public function index(): Response
    {
        return $this->render('index.html.twig');
    }
}
{# index.html.twig #}
{% extends '@EasyAdmin/layout.html.twig' %}

{% set _content_title = 'Dashboard' %}

{% block page_title -%}{{ _content_title }}{%- endblock %}

{% block content_header %}
    <h1 class="title">{{ _content_title }}</h1>
{% endblock content_header %}

{% block main %}Hi{% endblock main %}
<?php

declare(strict_types=1);

namespace App\Controller;

use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Environment;

final class IndexController implements DashboardControllerInterface
{
    private Environment $twig;

    public function __construct(Environment $twig)
    {
        $this->twig = $twig;
    }

    /**
     * @Route("/", name="app", methods={"GET"})
     */
    public function index(): Response
    {
        return new Response($this->twig->render('index.html.twig'));
    }

    public function configureDashboard(): Dashboard
    {
        return Dashboard::new();
    }

    public function configureAssets(): Assets
    {
        return Assets::new();
    }

    public function configureMenuItems(): iterable
    {
        return new \ArrayIterator();
    }

    public function configureUserMenu(UserInterface $user): UserMenu
    {
        return UserMenu::new();
    }

    public function configureCrud(): Crud
    {
        return Crud::new();
    }

    public function configureActions(): Actions
    {
        return Actions::new();
    }

    public function configureFilters(): Filters
    {
        return Filters::new();
    }
}

Problem not solved with your info. I think the problem with my current project. So now going to create a new test project. Will update soon with that info.
Maybe my current project may have some errors.

On a fresh symfony install it worked.
I had an error while installing easyadmin.

Warning: array_flip() expects parameter 1 to be array, null given
Location: vendor/easycorp/easyadmin-bundle/src/Registry/CrudControllerRegistry.php (line 34) 

I just comment line34 - ie array_flip(). Just to run the app for now.

The steps I done is.

  1. Created an empty folder
  2. cd to it
  3. composer create-project symfony/website-skeleton my_project_name
  4. tested with php -S localhost:8080 -t public
    4.1. Result success
    4.2. Make:controller
    4.3 Test - Result success
  5. Modified composer.json
    "easycorp/easyadmin-bundle": "dev-master",
  6. Asset install
    6.1 make:admin:dashboard
  7. Run throws the warning array_flip()
  8. I just comment it. (Refer above)
  9. Test it , run success.
  10. Used @LaKrue code. It worked.

I'm reopening because I want to look into this more in detail.

@javiereguiluz let me try to clarify non technically what brought this up in my case.

For me the "weird" part during my migration test was that i am used to extend the default EA2 layout/login template without taking care of anything else related to EA.
The Dashboard defined in the yaml config is not only the default but the only one.

Now, with multiple dashboards there is no such fallback and you need to be more clear on the context. That makes migration at least for my environment more work than expected. Thats no problem just my take away from like 2 hours testing .

And well, I got very confused that the old login template (and others) still exist and are documented, but are actually EA2 based and not useable/compatible at all. The moment i realised that I was already kind of debugging.

Having a pretty nice documentation for version 3 and being in alpha tricked me into thinking migration might work within a few hours - well it might be one or two hours more than expected :-)

Don't get me wrong, I can't wait to update soon. Really like the new concept for EA3!

Okey then let me post the difference between my old project and new(ea-running) project debug log.

In my project at /admin page the debug-toolbar->profiler->events->called listeners : There is no easyadmin events. But there is only one at debug-toolbar->profiler->events->not called listeners :

Symfony\Component\HttpKernel\Event\ViewEvent
--
0 | "EasyCorp\Bundle\EasyAdminBundle\EventListener\CrudResponseListener::onSymfonyComponentHttpKernelEventViewEvent()"

But in easyadmin working project its different.
At /admin page the debug-toolbar->profiler->events->called listeners :
Here only one is there.

kernel.controller
0 | "EasyCorp\Bundle\EasyAdminBundle\EventListener\AdminContextListener::onKernelController(ControllerEvent $event): void"

At /admin page the debug-toolbar->profiler->events->not called listeners :
Here too only one is there.

kernel.view
0 | "EasyCorp\Bundle\EasyAdminBundle\EventListener\CrudResponseListener::onKernelView(ViewEvent $event)"

Finally I found the problem and the solution. The event is not loading. So I solved by a small edit.
Easyadmin service file:
vendor/easycorp/easyadmin-bundle/src/Resources/config/services.php
on line : 134
Before:

->set(AdminContextListener::class)
            ->arg(0, ref(AdminContextFactory::class))
            ->arg(1, ref('controller_resolver'))
            ->arg(2, ref('twig'))
            ->tag('kernel.event_listener', ['event' => ControllerEvent::class])

After:

->set(AdminContextListener::class)
            ->arg(0, ref(AdminContextFactory::class))
            ->arg(1, ref('controller_resolver'))
            ->arg(2, ref('twig'))
            ->tag('kernel.event_listener', ['event' => "kernel.controller"])

This small change worked for me. Now my project running good.

Also change this

->set(CrudResponseListener::class)
            ->arg(0, ref(AdminContextProvider::class))
            ->arg(1, ref('twig'))
            ->tag('kernel.event_listener', ['event' => "kernel.view"])

By me nothing change: Variable "ea" does not exist.

@javiereguiluz Please note this too. I'm getting warning here. https://github.com/EasyCorp/EasyAdminBundle/issues/3165#issuecomment-622667422
I'm using PHP7.4.3 , Symfony5.0.8

Ok work. After cache clear

@rogergerecke I got the same error first. Here is the point to follow.

  1. Run this: console debug:event-dispatcher
  2. On the above result at : "kernel.controller" event
    2.1 Check EasyCorp\Bundle\EasyAdminBundle\EventListenerAdminContextListener::onKernelController() Exist or not.
  3. Then cache clear. It worked for me.

INFO: If there is a CRUD Controller then https://github.com/EasyCorp/EasyAdminBundle/issues/3165#issuecomment-623050888 there is no warning

For every composer update, I have to manually update the core file. Only then it works for me. This is a bug. You have to update at core files.
Also I tried to change only the priority, (by only changing priority dodnt worked for me). No use.

Still its not solved. On current updates.
At line: 107 , you mentioned exception event like this.

->set(ExceptionListener::class)
            ->arg(0, ref(AdminContextProvider::class))
            ->arg(1, ref('twig'))
            ->tag('kernel.event_listener', ['event' => 'kernel.exception', 'priority' => -64])

So you can change this in line 136.

->set(AdminContextListener::class)
            ->arg(0, ref(AdminContextFactory::class))
            ->arg(1, ref(DashboardControllerRegistry::class))
            ->arg(2, ref('controller_resolver'))
            ->arg(3, ref('twig'))
            ->tag('kernel.event_listener', ['event' => 'kernel.controller'])

        ->set(CrudResponseListener::class)
            ->arg(0, ref(AdminContextProvider::class))
            ->arg(1, ref('twig'))
            ->tag('kernel.event_listener', ['event' => 'kernel.view'])

Why, you are not updating this?
On every update I have to change these. Only then it works for me.

me too

Is it a bug? Or symfony missconfiguration? @javiereguiluz

Solution: Update Symfony to 5.1 so its works by me.

Yes it worked for me too. Updated to Symfony v5.1.
So, I'm closing this. Thanks to all.

Hi guys.

I also waste a little bit hours with this problem because I tried to create controller with __invoke method. I don't know why but it doesn't work with invoke, and show this error. You must have normall method. Anybody know why? Of course I extended my controller.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ealenn picture Ealenn  路  3Comments

nickicool picture nickicool  路  4Comments

haithem-rihane picture haithem-rihane  路  4Comments

javiereguiluz picture javiereguiluz  路  4Comments

BigMichi1 picture BigMichi1  路  3Comments