简体   繁体   中英

How to include External classes for extensions in TYPO3?

I have an extension named Menu which requires the help of the class MenuHelper in the folder ext/menu/Classes/Helper .

How do I use it in my controller? The class 'MenuHelper' is being accessed only when it is in the controller folder.

I want to use it in my controller like this :

public function listAction() {

        $menugenerators=new MenuHelper(); # Will return something 

        $this->view->assign('menugenerators', $menugenerators);
}

If you place a class in the directory yourExt/Classes/Helper/NestedDirectory/MenuHelper.php you have two ways to get the class in all other extbase files.

First way with TYPO3 > 6.0: Using namespaces

<?php
namespace YourVendor\YourExtension\Helper\NestedDirectory;

class MenuHelper {
}

?>

Now you can make an instance of this class using

$menuHelper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("YourVendor\\YourExtension\\Helper\\NestedDirectory\\MenuHelper")

or simply by using

$menuHelper = new \YourVendor\YourExtension\Helper\NestedDirectory\MenuHelper();

Second way with TYPO3 <= 4.7

<?php

class Tx_YourExtension_Helper_NestedDirectory_MenuHelper {
}

?>

Now you can make an instance of this class using

$menuHelper = t3lib_div::makeInstance("Tx_YourExtension_Helper_NestedDirectory_MenuHelper")

or simply by using

$menuHelper = new Tx_YourExtension_Helper_NestedDirectory_MenuHelper();

The name of the class directs the class-loader to the correct path of the source file. It is very important that the file has the same name the class has.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM