-1

I updated the dependency Injection in my Controller, now it looks like this:

class MyExtensionController extends ActionControlelr
{
 /**
 * @ var MyExtensionRepository
 /**
 protected $myRepository
..
..
..

But calling the Repository in the next function like this

public function indexAction()
{
    // get needed info
    /** @var Category $category */
    $category = $this->myRepository->findByUid($this->settings['flexForm']['categoryId']);

returns the error:

Error: Call to a member function findByUid() on null

Services.yaml looks like this:

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  REP\MyExtension\:
    resource: '../Classes/*'

Any ideas?

Thanks

Victor MGE
  • 41
  • 6

2 Answers2

2

It looks like you are mixing up TYPO3 and Symphony. Everything except Services.yaml looks like TYPO3.

In a TYPO3 Controller you have to inject the Repository. You can do this by using the @inject annonation next to the variable declaration. You should also use a full namespace there. Then you have to clear all caches and it should work.

This will only work for TYPO3 Versions below 9

Please fix up your whole comment section.

    /**
     * @var \Full\Path\To\Repository\MyExtensionRepository
     * @inject
     */
     protected $myRepository

For Version 9 and higher please use the new depency injection https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.0/Feature-82869-ReplaceInjectWithTYPO3CMSExtbaseAnnotationInject.html

Paul Beck
  • 2,675
  • 1
  • 12
  • 14
  • 3
    Notice that you should use the newer annotation for injection: https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.0/Feature-82869-ReplaceInjectWithTYPO3CMSExtbaseAnnotationInject.html – Mathias Brodala Jan 20 '20 at 11:39
  • 2
    Hi,thanks for the fast answers. If I am not wrong, starting in Typo3 10 the dependency injection from symfony has to be used https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/10.0/Feature-84112-SymfonyDependencyInjectionForCoreAndExtbase.html and the @inject notation is going to be deprecated in future releases. – Victor MGE Jan 20 '20 at 12:12
  • Thanks @MathiasBrodala I've attached a notice regarding the version. – Paul Beck May 07 '21 at 08:45
1

Ok, I found the error. Apparently for the Dependency Injections a constructor has to be implemented. In this case adding

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

works like a charm :)

Victor MGE
  • 41
  • 6