0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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.

5 Comments

Possible that it is out of the date and this is my problem.. To the other part, I have a controller with the listAction(). I could output the json there and use a pageType with disableAllHeaderCode. But it will still load the template and possible html comments / or an error when there is no template. Can I somehow could disable the template (and the rendering) ?
Add a return statement to your listAction. You could return your JSON object here as string. TYPO3 evaluates the return data type of your action and if it's void then it's try to render the view. So just return something, even a empty string and the view->render method will not be called which generates this error due to the missing List.html template ;)
Thats not the problem here. You @nbar wouldnt get to the point of this error if the action wouldn't exist or wouldn't be executed.
@Lasse Yes I found out that when I have an empty List.html + some Typoscript to disable HTML Comments this works. Thanks
You're using it wrong. Point to your own view instead of initiating the template view if theres nothing to render.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.