Controlling the flow

The frontend page shall show a list of products in the inventory. The component responsible for rendering that list is the view. The default view and templating engine used in Extbase is Fluid.

The connection between the model and the view is the controller. The controller is responsible for fetching the model’s data and handing it to the view to be rendered. The controller uses *Action methods as entry points. In our case, we want to display a list of products, so we should implement a listAction.

The Controller

The class name of the controller must end with Controller. Because our controller controls the display of the inventory we call it \T3docs\StoreInventory\Controller\StoreInventoryController.

The controller looks like this:


namespace T3docs\StoreInventory\Controller;

use T3docs\StoreInventory\Domain\Repository\ProductRepository;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use Psr\Http\Message\ResponseInterface;

class StoreInventoryController extends ActionController
   private $productRepository;

   public function injectProductRepository(ProductRepository $productRepository)
      $this->productRepository = $productRepository;

   public function listAction(): ResponseInterface
      $products = $this->productRepository->findAll();
      $this->view->assign('products', $products);
      return $this->htmlResponse();

Changed in version 11.0: From version 11 on Extbase expects actions to return an instance of PsrHttpMessageResponseInterface.

Our \T3docs\StoreInventory\Controller\StoreInventoryController is derived from \TYPO3\CMS\Extbase\Mvc\Controller\ActionController. It only contains the method listAction(). Extbase identifies all methods ending with Action as actions - entry points to our application.

Injecting the repository

The method injectProductRepository() shows how dependency injection looks like in Extbase - Extbase automatically injects the product repository via this method. Afterwards the repository can be accessed with $this->productRepository in all actions. Use dependency injection for getting all of your class dependencies if possible.

Fetching the products from the repository

As a list of all products should be displayed in our inventory, the method findAll() of the repository is used to fetch them. findAll() is implemented in class \TYPO3\CMS\Extbase\Persistence\Repository, the parent class of our repository.

Assigning the view

The repository returns a \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult object with all product objects (that are not hidden or deleted). These objects are passed to the view with $this->view->assign(…). The view will automatically call render() and return the rendered template.

return $this->view->render();