7

I'm trying to get started with TYPO3 extensions and was following this tutorial to get to see the basics.

In the backend everything works fine, but on the front end I get an error:

Oops, an error occurred! Code: 20170209104827c3b58d58 - {"exception":"exception 'ReflectionException' with message 'Class Tx_Inventory_Controller_InventoryController does not exist'

My files are exactly the same as in the tutorial. I have no idea what is causing this. I assume I made some dumb mistake with namespaces, but they seem to be all correct.

The controller class can be found below and is located in typo3conf/ext/inventory/Classes/Controller/

<?php
   namespace \MyVendor\Inventory\Controller;
   use \TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
   use \TYPO3\CMS\Core\Utility\GeneralUtility;
   use \MyVendor\Inventory\Domain\Model\Repository\ProductRepository;

   class InventoryController extends ActionController {
        public function listAction() {
                $productRepository = GeneralUtility::makeInstance(ProductRepository::class)
                $products = $productRepository->findAll();
                $this->view->assign('products', $products);
        }
   }
?>
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Woeler
  • 227
  • 4
  • 14
  • Please post the top of your Controller file typo3conf/ext/inventory/Classes/Controller/InventoryController.php? Maybe you have wrong syntax in namespace or classname. Ensure, you have loaded your classes, which are not automaticly loaded, if you just create/upload it. Best would be, to deinstall/reinstall your EXT:inventory if you add new PHP class files. – jokumer Feb 09 '17 at 10:12
  • Thanks of the reply! I added it to the post. – Woeler Feb 09 '17 at 10:20

5 Answers5

10

When developing a new extension in a composer installed TYPO3 V9 (here: 9.4) the autoload part has to be added to the central root composer.json. Found it here (German). Following the steps in the OPs mentioned tutorial leads to a core exception:

Core: Exception handler (WEB): Uncaught TYPO3 Exception: #1278450972:
  Class MyVendor\StoreInventory\Controller\StoreInventoryController does not exist.
  Reflection failed.

As long as the extension is not installed via composer, e.g because it's newly developed, composer does not find the appropriate composer.json file in the extensions directory. Hence TYPO3 does not find any classes in the new extensions Classes directory. To resolve the issue the autoload configuration has to be added to the root composer.json. Just put the following lines into composer.json within the installations base directory:

{
    "repositories": [
        { "type": "composer", "url": "https://composer.typo3.org/" }
    ],
    ...
    "autoload": {
        "psr-4": {
            "MyVendor\\StoreInventory\\": "public/typo3conf/ext/store_inventory/Classes/"
        }
    }
}

Then regenerate the autoload configuration:

composer dumpautoload

You possibly have to clear the cache as well in the backend.

Holger Böhnke
  • 971
  • 11
  • 17
4

It looks like your class is not autoloaded. If you don't use composer to make your autoload, take a look in your typo3conf/autoload/autoload_classmap.php file.

You should find an entry corresponding to your file. You will see if you have a path error.

Maria
  • 4,471
  • 1
  • 25
  • 26
Pierre Fru
  • 343
  • 2
  • 7
3

Remove backslashes - try with

<?php
namespace MyVendor\Inventory\Controller;

use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use MyVendor\Inventory\Domain\Model\Repository\ProductRepository;

class InventoryController extends ActionController {

    public function listAction() {
            $productRepository = GeneralUtility::makeInstance(ProductRepository::class)
            $products = $productRepository->findAll();
            $this->view->assign('products', $products);
    }
}

Ensure you add Vendorname to extension key, when you register your plugin, see ext_tables.php and write 'MyVendor.'.$_EXTKEY instead of $_EXTKEY like

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
    'MyVendor.'.$_EXTKEY,
    'List',
    'The Inventory List'
);
jokumer
  • 2,249
  • 11
  • 22
  • Thanks, but no luck. I have the feeling the controller is ok, but Typo3 is looking in the wrong place. Also the error says Class Tx_Inventory_Controller_InventoryController, which is the old way of naming classes, right? – Woeler Feb 09 '17 at 10:30
  • Which TYPO3 version? – jokumer Feb 09 '17 at 10:32
  • TYPO3 Version 7.6.15 – Woeler Feb 09 '17 at 10:33
  • 1
    I see a minor mistake in the documentation you linked. Change $_EXTKEY in ext_tables.php, where you register your plugin. For "\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin($_EXTKEY,..." write "\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin('MyVendor.inventory',..." – jokumer Feb 09 '17 at 10:39
  • Was two mistakes you need to correct in your syntax - as written/edited in my answer – jokumer Feb 09 '17 at 11:05
  • Do not replace `$_EXTKEY`. It will create all sorts of problems if you set this to a fixed string, especially in composer mode. – pgampe Feb 09 '17 at 23:26
0

I had exactly the same problem - it happens if Typo3 installation is done by composer. To solve this problem see this page of the docs.

William Miller
  • 9,839
  • 3
  • 25
  • 46
0

Try to add autoload in your ext_emconf.php (replace 'Vendor\\Extensionkey\\') and uninstall and install your extension again (to rebuild PHP autoload information)

  'autoload' =>
    array (
        'psr-4' =>
            array (
                'Vendor\\Extensionkey\\' => 'Classes',
            ),
    ),
    '_md5_values_when_last_written' => 'a:0:{}',
    'suggests' => array(
),
Christian Michael
  • 2,128
  • 1
  • 19
  • 27