简体   繁体   中英

EasyAdmin :: CrudUrlGenerator Symfony 6 is not working properly

I am using Symfony 6 and EasyAdmin Bundle. The dashboard of symfony is empty and has no data to display due to that I use the dashboard to show the meetings.

I wrote the following code

namespace App\Controller\Admin; 

use App\Entity\Comment; 
use App\Entity\Conference; 
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard; 
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem; 
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route;

class DashboardController extends AbstractDashboardController 
{
        #[Route('/admin', name: 'admin')]
        public function index(): Response
        {
            $routeBuilder = $this->get(CrudUrlGenerator::class)->build();
            $url = $routeBuilder->setController(MeetingCrudController::class)->generateUrl();
    
            return $this->redirect($url);
        }
}

But it gives me an error that

  1. Method 'get' not found in DashboardController
  2. Undefined class 'CrudUrlGenerator'

Please how I can solve those issues

You can inject the AdminUrlGenerator to the constructor and use it in the index method

private AdminUrlGenerator $adminUrlGenerator;

public function __construct(AdminUrlGenerator $adminUrlGenerator)
{
    $this->adminUrlGenerator = $adminUrlGenerator;
}

public function index()
{
    $url = $this->adminUrlGenerator
            ->setController(MeetingCrudController::class)
            ->generateUrl();

    return $this->redirect($url);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM