Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Decouple from CRUDController

Decouple from CRUDController

Edit this page

When creating custom actions, we can create our controllers without extending CRUDController. What we usually need is to access the admin instance associated to the action, to do so we can type-hint to the admin class or use the AdminFetcherInterface service.

You can add your Admin as parameter of the action:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// src/Controller/CarAdminController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;

final class CarAdminController
{
    public function clone(CarAdmin $admin, Request $request)
    {
        $object = $admin->getSubject();

        // ...

        $request->getSession()->getFlashBag()->add('sonata_flash_success', 'Cloned successfully');

        return new RedirectResponse($admin->generateUrl('list'));
    }
}

Or if you have a reusable action for all admin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// src/Controller/CarAdminController.php

namespace App\Controller;

use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

final class CloneAdminController
{
    public function clone(AdminInterface $admin, Request $request)
    {
        $object = $admin->getSubject();

        // ...

        $request->getSession()->getFlashBag()->add('sonata_flash_success', 'Cloned successfully');

        return new RedirectResponse($admin->generateUrl('list'));
    }
}

Or you can use AdminFetcherInterface service to fetch the admin from the request, in this example we transformed the controller to make it Invokable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// src/Controller/CarAdminController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;

final class CarAdminSoldAction
{
    private AdminFetcherInterface $adminFetcher;

    public function __construct(AdminFetcherInterface $adminFetcher)
    {
        $this->adminFetcher = $adminFetcher;
    }

    public function __invoke(Request $request)
    {
        $admin = $this->adminFetcher->get($request);

        $object = $admin->getSubject();

        // ...

        $request->getSession()->getFlashBag()->add('sonata_flash_success', 'Sold successfully');

        return new RedirectResponse($admin->generateUrl('list'));
    }
}

Now we only need to add the new route in configureRoutes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use App\Controller\CarAdminCloneAction;
use Sonata\AdminBundle\Route\RouteCollectionInterface;

protected function configureRoutes(RouteCollectionInterface $collection)
{
    $collection
        ->add('clone', $this->getRouterIdParameter().'/clone', [
            '_controller' => 'App\Controller\CarAdminController::clone',
        ])

        // Using invokable controller:
        ->add('sold', $this->getRouterIdParameter().'/sold', [
            '_controller' => CarAdminSoldAction::class,
        ]);
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Take the exam at home

    Take the exam at home

    Be safe against critical risks to your projects and businesses

    Be safe against critical risks to your projects and businesses

    Version:

    Symfony footer

    Avatar of Matthias Schmidt, a Symfony contributor

    Thanks Matthias Schmidt for being a Symfony contributor

    2 commits • 6 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony