Trupti
Trupti

Reputation: 983

In Symfony, how to call controller function in Menubuilder?

I have written one function as getAccess() in controller file Appbundle/Controller/BackendController.php.

I want to access this controller's method in Menu/Menubuilder.php file. How can I do that?

Menu and Appbundle folders are at same level.

Upvotes: 0

Views: 304

Answers (3)

Trupti
Trupti

Reputation: 983

I have created service as follows:

namespace AppBundle\Services;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;


class UserAccessService {

    private $conn;
    private $container;
    private $tokenStorage;

    public function __construct(EntityManagerInterface $entityManager, ContainerInterface $container, TokenStorageInterface $tokenStorage) {
        $this->conn = $entityManager;
        $this->container = $container;
        $this->tokenStorage = $tokenStorage;
    }

and added following code in services.yml:

app.service.useraccessservice:
        class: AppBundle\Services\UserAccessService
        arguments: ['@doctrine.orm.default_entity_manager','@service_container','@security.token_storage']

    app.menu_builder:
        class: AppBundle\Menu\MenuBuilder
        arguments: ["@knp_menu.factory", "@security.authorization_checker", '@security.token_storage', '@translator', '@app.service.useraccessservice','@kernel']
        public: true
        tags:
            - { name: knp_menu.menu_builder, method: createMainMenu, alias: main_menu }
            - { name: knp_menu.menu_builder, method: createManagementMenu, alias: management_menu }
            - { name: knp_menu.menu_builder, method: createUserMenu, alias: user_menu }

It works as expected.

Upvotes: 0

Mitesh Vasava
Mitesh Vasava

Reputation: 735

You can use Trait

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

So, you can create your function getAccess() in trait file and just use it in BackendController.php and Menubuilder.php

trait ezcReflectionReturnInfo {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class ezcReflectionMethod extends ReflectionMethod {
    use ezcReflectionReturnInfo;
    /* ... */
}

class ezcReflectionFunction extends ReflectionFunction {
    use ezcReflectionReturnInfo;
    /* ... */
}

Upvotes: 2

Heyden7611
Heyden7611

Reputation: 174

For me, a controller can not be called in menuBuilder and it would not be "clean". I suggest you create a manager or service that contains this feature and call your service in your controller and in MenuBuilder.

namespace App\Service;

class MessageGenerator
{
    public function getHappyMessage()
    {
        $messages = [
            'You did it! You updated the system! Amazing!',
            'That was one of the coolest updates I\'ve seen all day!',
            'Great work! Keep going!',
        ];

        $index = array_rand($messages);

        return $messages[$index];
    }
}

What version of symfony are you on?

Upvotes: 2

Related Questions