如何为任何路由设置控制器以获取所有控制器中的基本对象

时间:2019-07-19 11:08:24

标签: symfony controller twig

我正在构建自己的CMS系统。而且我过去在symfony上做了很多工作,现在我想做更多的事情:)我想为管理仪表板呈现基本控制器,其中包含菜单,“ / admin”路线上的系统通知,然后我想在路由“ / admin / test”上设置另一个控制器,例如“ test”,我的问题是,名为AdminController的控制器的所有对象通知在第二条路由“ / admin / test”中不可用,仅在路由“ / admin”上可用/ p>

这是我的adminControler控制器:

class AdminController extends Controller
{
    /**
     * @Route("/admin", name="adminDashboard")
     */
    public function adminDashboard()
    { 

        $loggedUser = $this->getUser()->getId();

        $systemnotifications = $this->forward('App\Controller\SystemNotificationController::notif', [
        'loggedUser' => $loggedUser

        ]);
        return $this->render('admin/index.html.twig', [
                'systemnotifications' => $systemnotifications
        ]);

    }

}

这里是我的测试控制器:

class TestController extends Controller
{


    /**
     * @Route("/admin/test", name="test")
     */
    public function test()
    {
        return $this->render('admin/dashboard/index.html.twig', [

        ]);

    }
}

在设置了twig之后,adminController扩展了base.html.twig,而Test控制器扩展了index.html.twig(这一个女巫是从adminController呈现的。

我的问题是如何使用Symfony最佳实践正确处理它。我应该如何设置管理控制器以获取systemnotifications对象,在哪里启动了另一个测试控制器?

请帮助:)

2 个答案:

答案 0 :(得分:0)

有两种方法可以做到,第一种是在变量中插入变量。示例,请参见此doc

# config/packages/twig.yaml
twig:
    # ...
    globals:
        # the value is the service's id
        user_management: '@App\DataProvider\UserDataProvider'
# config/services.yaml
services:
   'App\DataProvider\UserDataProvider':
        arguments:
            - '@session'
        autoconfigure: false

答案 1 :(得分:0)

另一种方式则更加复杂,例如,如果您希望负责呈现页面的特定部分,例如... barnav或消息

将这段代码添加到默认的树枝中:

{% block user_control %}
  {{ render(controller('LayoutCoreBundle:User:index')) }}
{% endblock %}
<?php
namespace App\Controller;

use App\Event\ShowUserEvent;
use App\Event\ThemeEvents;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use \stdClass;

class UserController extends EmitterController
{
    /**
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function indexAction()
    {
        if (!$this->getDispatcher()->hasListeners(ThemeEvents::THEME_NAVBAR_USER)) {
            return new Response();
        }
        /** @var ShowUserEvent $userEvent */
        $userEvent = $this->triggerMethod(ThemeEvents::THEME_NAVBAR_USER, new ShowUserEvent());
        $userClass = $userEvent->getUser();
        $user                       = new stdClass();
            $user->id               = $userClass->getIdentifier();
            $user->idEmployee       = $userClass->getIdEmployee();
            $user->setCompanyLogo   = $userClass->getCompanyLogo();
            $user->companyName      = $userClass->getCompanyName();
            $user->company          = $userClass->getCompany();
            $user->avatar           = $userClass->getAvatar();
            $user->fullName         = $userClass->getName();
            $user->menu             = $userClass->getMenu();
            $user->role             = $userClass->getRolname();
        return $this->render(
            'header/index.html.twig',
            [
                'userJson' => $user,
            ]
        );
    }

}

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EmitterController extends AbstractController
{
    /**
     * @var EventDispatcherInterface
     */
    protected $eventDispatcher;

    /**
     * @param EventDispatcherInterface $dispatcher
     */
    public function __construct(EventDispatcherInterface $dispatcher)
    {
        $this->eventDispatcher = $dispatcher;
    }

    /**
     * @return EventDispatcherInterface
     */
    protected function getDispatcher()
    {
        return $this->eventDispatcher;
    }

    /**
     * @param string $eventName
     *
     * @return bool
     */
    protected function hasListener($eventName)
    {
        return $this->getDispatcher()->hasListeners($eventName);
    }

    /**
     * Will look for a method of the format "on<CamelizedEventName>" and call it with the event as argument.
     *
     *
     * Then it will dispatch the event as normal via the event dispatcher.
     *
     * @param $eventName
     * @param Event $event
     *
     * @return Event
     */
    protected function triggerMethod($eventName, Event $event)
    {
        $method = sprintf('on%s', Container::camelize(str_replace('.', '_', $eventName)));

        if (is_callable([$this, $method])) {
            call_user_func_array([$this, $method], [$event]);
        }

        if ($event->isPropagationStopped()) {
            return $event;
        }

        $this->getDispatcher()->dispatch($eventName, $event);

        return $event;
    }
}

interface ThemeEvents
{
    /**
     * Used to receive notification data
     */
    public const THEME_NOTIFICATIONS = 'theme.notifications';
    /**
     * Used to receive message data
     */
    public const THEME_MESSAGES = 'theme.messages';
    /**
     * Used to receive task data
     */
    public const THEME_TASKS = 'theme.tasks';
    /**
     * Used to receive the current user for the navbar
     */
    public const THEME_NAVBAR_USER = 'theme.navbar_user';
    /**
     * Used to receive breadcrumb data
     */
    public const THEME_BREADCRUMB = 'theme.breadcrumb';
    /**
     * Used to receive the current user for the sidebar
     */
    public const THEME_SIDEBAR_USER = 'theme.sidebar_user';
    /**
     * Used to receive the sidebar menu data
     */
    public const THEME_SIDEBAR_SETUP_MENU = 'theme.sidebar_setup_menu';
}
class ShowUserEvent extends ThemeEvent
{
    /**
     * @var UserInterface
     */
    protected $user;

    /**
     * @var bool
     */
    protected $showProfileLink = true;

    /**
     * @var bool
     */
    protected $showLogoutLink = true;

    /**
     * @var NavBarUserLink[]
     */
    protected $links = [];

    /**
     * @param UserInterface $user
     * @return ShowUserEvent
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * @return UserInterface
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @return NavBarUserLink[]
     */
    public function getLinks()
    {
        return $this->links;
    }

    /**
     * @param NavBarUserLink $link
     * @return ShowUserEvent
     */
    public function addLink(NavBarUserLink $link)
    {
        $this->links[] = $link;

        return $this;
    }

    /**
     * @return bool
     */
    public function isShowProfileLink()
    {
        return $this->showProfileLink;
    }

    /**
     * @param bool $showProfileLink
     * @return ShowUserEvent
     */
    public function setShowProfileLink($showProfileLink)
    {
        $this->showProfileLink = $showProfileLink;

        return $this;
    }

    /**
     * @return bool
     */
    public function isShowLogoutLink()
    {
        return $this->showLogoutLink;
    }

    /**
     * @param bool $showLogoutLink
     * @return ShowUserEvent
     */
    public function setShowLogoutLink($showLogoutLink)
    {
        $this->showLogoutLink = $showLogoutLink;

        return $this;
    }
}
class ThemeEvent extends Event
{
}

然后只有您需要偶数订阅者

class NavbarUserSubscriber implements EventSubscriberInterface
{
    /**
     * @var Security
     */
    protected $security;

    /**
     * @param Security $security
     */
    public function __construct(Security $security)
    {
        $this->security = $security;
    }

    /**
     * @return array
     */
    public static function getSubscribedEvents(): array
    {
        return [
            ThemeEvents::THEME_NAVBAR_USER => ['onShowUser', 100],
            ThemeEvents::THEME_SIDEBAR_USER => ['onShowUser', 100],
        ];
    }
    /**
     * @param ShowUserEvent $event
     * @throws \Exception
     */
    public function onShowUser(ShowUserEvent $event)
    {
        if (null === $this->security->getUser()) {
            return;
        }
        /** @var User $user */
        $user = $this->security->getUser();
        $event->setUser($user);
    }
}

#templates/header/index.html.twig
<script type="text/javascript">
var user = {{ userJson | json_encode() | raw  }};
</script>