Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Symfony中,如何在Menubuilder中调用控制器函数?_Php_Symfony - Fatal编程技术网

Php 在Symfony中,如何在Menubuilder中调用控制器函数?

Php 在Symfony中,如何在Menubuilder中调用控制器函数?,php,symfony,Php,Symfony,我在控制器文件Appbundle/controller/BackendController.php中编写了一个函数getAccess() 我想在菜单/Menubuilder.php文件中访问此控制器的方法。我该怎么做 菜单和Appbundle文件夹处于同一级别。对于我来说,无法在menuBuilder中调用控制器,而且它不会“干净”。我建议您创建一个包含此功能的管理器或服务,并在控制器和MenuBuilder中调用您的服务 namespace App\Service; class Messag

我在控制器文件
Appbundle/controller/BackendController.php
中编写了一个函数getAccess()

我想在
菜单/Menubuilder.php
文件中访问此控制器的方法。我该怎么做


菜单和Appbundle文件夹处于同一级别。

对于我来说,无法在menuBuilder中调用控制器,而且它不会“干净”。我建议您创建一个包含此功能的管理器或服务,并在控制器和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];
    }
}
您使用的是什么版本的symfony?

您可以使用

Traits是单一继承语言(如PHP)中代码重用的一种机制。Trait旨在通过使开发人员能够在不同类层次结构中的几个独立类中自由地重用方法集来减少单个继承的一些限制

所以,您可以在trait文件中创建函数getAccess(),并在BackendController.php和Menubuilder.php中使用它

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

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

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

我创建了如下服务:

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;
    }
并在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 }

它工作正常。

Symfony版本是3.4,虽然建议提取getAccess到它自己的服务是好的,但发布的代码似乎不相关。这是一个示例,我将给出他可能有的回应,但是我们可以看到getAccess()的代码吗?@Heyden7611,谢谢:)您应该将此函数getAccess()提取到单独的服务中,并通过依赖项注入进行传递。