[英]In Symfony, how to call controller function in Menubuilder?
I have written one function as getAccess() in controller file Appbundle/Controller/BackendController.php
. 我已经在控制器文件
Appbundle/Controller/BackendController.php
编写了一个作为getAccess()的函数。
I want to access this controller's method in Menu/Menubuilder.php
file. 我想在
Menu/Menubuilder.php
文件中访问此控制器的方法。 How can I do that? 我怎样才能做到这一点?
Menu and Appbundle folders are at same level. 菜单和Appbundle文件夹处于同一级别。
Traits are a mechanism for code reuse in single inheritance languages such as PHP. 特性是一种在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 因此,您可以在特征文件中创建函数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;
/* ... */
}
For me, a controller can not be called in menuBuilder and it would not be "clean". 对我来说,不能在menuBuilder中调用一个控制器,它也不是“干净的”。 I suggest you create a manager or service that contains this feature and call your service in your controller and in 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];
}
}
What version of symfony are you on? 您正在使用什么版本的symfony?
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: 并在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. 它按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.