5

是否可以在不同的控制器中调用函数?我需要调用FindByCategoryGrouped($catId)designRepository.php 和getCategories($catId)categoryRepository.php

public function listAction() {
    $this->settings['flexform']['showCategory'] ? $catId = $this->settings['flexform']['showCategory']:$catId = $this->settings['listView']['showCategory'];
    // print $catId;
    if (!$catId || $this->settings['flexform']['showCategory'] == '-1') {
        $designs = $this->designRepository->findAll(); 
    } else {
        // $designs = $this->designRepository->findByCategory($catId);
        $designs = $this->designRepository->findByCategoryGrouped($catId);  // THIS
        $categories = $this->categoryRepository->getCategories($catId); // THIS
    }
    // indhold forsvinder hvis næste linje slettes
    $this->view->assign('designs', $designs, "L", $GLOBALS['TSFE']->sys_language_uid);
    $this->view->assign('catId', $catId);
    $this->view->assign('categories', $categories);
}
4

1 回答 1

23

您可以注入每个已安装的 extbase 扩展的每个存储库。只需将依赖注入代码添加到您的控制器。根据您的 TYPO3 版本:

TYPO3 >= 6.0:

/**
 * @var \Vendor\Extension\Domain\Repository\SomeRepository
 * @inject
 */
protected $someRepository;

请注意,与@inject专用注入方法相比,注释的性能不是很好。因此,如果您需要调整应用程序的性能并且在您的控制器中有许多注入,您应该考虑切换到注入方法:

/**
 * @var \Vendor\Extension\Domain\Repository\SomeRepository
 */
protected $someRepository;

/**
 * @param \Vendor\Extension\Domain\Repository\SomeRepository
 */
public function injectSomeRepository(\Vendor\Extension\Domain\Repository\SomeRepository $someRepository) {
  $this->someRepository = $someRepository;
}

TYPO3 = 4.7:

/**
 * @var Tx_MyExtension_Domain_Repository_SomeRepository
 * @inject
 */
 protected $someRepository;

TYPO3 < 4.7

/**
 * @var Tx_MyExtension_Domain_Repository_SomeRepository
 */
 protected $someRepository;

/**
 * Inject SomeRepository
 * @param Tx_MyExtension_Domain_Repository_SomeRepository $someRepository
 * @return void
 */
public function injectSomeRepository(Tx_MyExtension_Domain_Repository_SomeRepository $someRepository) {
  $this->someRepository = $someRepository;
}

在任何情况下,您都可以$this->someRepository在将存储库注入的控制器中使用其所有方法。

编辑:修正错字。

编辑: 添加依赖注入后,您必须清除缓存!

于 2013-11-01T13:21:37.367 回答