nbar
nbar

Reputation: 6158

Typo3 Extension PHP View

Using the infos in this link:
https://docs.typo3.org/typo3cms/ExtbaseFluidBook/8-Fluid/9-using-php-based-views.html
I try to create an action to output a JSON.

I have a normal controller with the list action:

public function listAction()
{
    $storelocators = $this->storelocatorRepository->findAll();
    $this->view->assign('storelocators', $storelocators);
}

And in ext/my_storelocator/Classes/View/Storelocator I have a class List.php:

<?

class Tx_MyStorelocator_View_Storelocator_List extends Tx_Extbase_MVC_View_AbstractView {
        public function render() {
                return 'Hello World';
        }
}

All I get is:

Sorry, the requested view was not found.

The technical reason is: No template was found. View could not be resolved for action "list" in class "My\MyStorelocator\Controller\StorelocatorController".

So I guess there is something wrong with the paths. Or where is the Problem?

Edit: Extensioninfos

Vendor: My
key: my_storelocator
controller: NOT SURE (I created it with the extension_builder so I guess my controllers name is Storelocator)
action: list

From my understanding a classname like Tx_MyStorelocator_View_Storelocator_List should be correct. But its not working

Upvotes: 0

Views: 730

Answers (3)

j4k3
j4k3

Reputation: 1201

First of all, TYPO3 now has a built-in JSON view, described thoroughly here: https://usetypo3.com/json-view.html. It lets you easily define which properties you'd like to render.

The error message means that your Controller is still pointing to the TemplateView - because thats the error the TemplateView throws if it can't find the defined template file.

You can specify which view to use to render within your controller. You can either set a default view via the $defaultViewObjectName property, like so:

/**
 * @var string
 */
protected $defaultViewObjectName = '\TYPO3\CMS\Fluid\View\TemplateView';

You can also set it from within the Controller inside initialization actions like so:

public function initializeExportPDFAction(){
    $this->defaultViewObjectName = 'Vendor\Extension\View\FileTransferView';
}

(I have, however, not yet found a way to define the template from within actions, any tips in the comments would be appreciated)

Upvotes: 1

pgampe
pgampe

Reputation: 4578

You will need to create an empty file for the HTML view for your controller, e.g. Resources/Private/Template/Storelocator/List.html, even if you do not plan to use the HTML view or if you just return the content yourself (which is perfectly fine).

The reason for this is simply technical limitation.

Upvotes: 1

Andrew
Andrew

Reputation: 605

Your path syntax is probably out of date. Instead of writing a render() function in Classes/View/Storelocator/List.php, try writing a listAction() function in a Classes/Controller/StorelocatorController.php file. Extension Builder should have created this file for you, if you made an aggregate model with the usual "list, create, edit ..." and such actions.

Review A journey through the Blog Example and the following chapter, Creating a first extension, for tips.

Keep in mind that there is a mismatch between the documentation and the Extension Builder generated PHP code files. Developing TYPO3 Extensions with Extbase and Fluid has some parts up to date, and other parts still using old syntax.

Upvotes: 0

Related Questions