链接到其他CRUD控制器的某些动作

发布于 2025-01-28 04:20:55 字数 938 浏览 3 评论 0原文

在用easyAdmin 4.x制成的CRUD控制器中,是否有任何解决方法可以将新操作链接到另一个具有OneTomany关系的CRUD控制器中的操作?

class FirstEntityCrudController extends AbstractCrudController
{
...
public function configureActions(Actions $actions): Actions
{
    return $actions
        ->add(Crud::PAGE_INDEX, Action::new('add-second-entity','Add a second entity')
        ->linkToCrudAction(Action::NEW ???)

            )
        ;
    }
}

docs 我可以使用:

linkTocrudaction():执行当前CRUD控制器的某些方法;

但是,似乎没有任何迹象表明如何“执行某种 crud控制器的方法”。

笔记: 有一种偷偷摸摸的方法,但似乎不健康:

   ->linkToUrl('the url to the desired action')
                

使用:

  • PHP 8.1
  • Symfony 5.4
  • EasyAdmin 4.x

Is there any workaround to link a new action, in a CRUD controller made with EasyAdmin 4.x, to an action in another CRUD controller with which it has a OneToMany relation ?

class FirstEntityCrudController extends AbstractCrudController
{
...
public function configureActions(Actions $actions): Actions
{
    return $actions
        ->add(Crud::PAGE_INDEX, Action::new('add-second-entity','Add a second entity')
        ->linkToCrudAction(Action::NEW ???)

            )
        ;
    }
}

The docs say that I can use:

linkToCrudAction(): to execute some method of the current CRUD controller;

But there seems to be no indication on how to "execute some method of a different CRUD controller".

Note:
There is a sneaky way around it but it doesn't seem healthy :

   ->linkToUrl('the url to the desired action')
                

Using:

  • PHP 8.1
  • Symfony 5.4
  • EasyAdmin 4.x

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2025-02-04 04:20:55

以下 @ruban 的评论,然后AS easyadminbundle 的文档提及,我们可以使用Adminurlgenerator类为所需操作生成一个URL,如下所示

class FirstEntityCrudController extends AbstractCrudController
{
private $adminUrlGenerator;

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

// ...

public function configureActions(Actions $actions): Actions
{
// The magic happens here

Following @Ruban's comment, and as EasyAdminBundle's docs mention, we can generate a URL for the desired action using the AdminUrlGenerator class as follow:

class FirstEntityCrudController extends AbstractCrudController
{
    private $adminUrlGenerator;

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

    // ...

    public function configureActions(Actions $actions): Actions
    {
        // The magic happens here ????
        $url = $this->adminUrlGenerator
            ->setController(SecondEntityCrudController::class)
            ->setAction(Action::NEW)
            ->generateUrl();

        return $actions
         // ->... 
            ->add(Crud::PAGE_INDEX, 
                  Action::new('add-second-entity', 'Add second entity')
                ->linkToUrl($url)
            );
    }
}

This worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文