typo3dev
typo3dev

Reputation: 107

TYPO3 Webservice/Rest API as Repository for Extbase Extension

I am developing an TYPO3 extbase extension to connect to my external application that has a REST-API. All I want is to retrieve the data from my REST-API and pass this data to the controller. Since I am quite new to extbase development, I didn't really find any resources about Repository interacting with a webservice. Only documentation about Repository that interacts with a database (MySQL, PostgreSQL, ..) I would like to know, where should I place the cURL-Request Function to connect to the API? In the Model? In the Repository? How can the Controller in my Extension access that data? From Model or Repository?

What would be the best practice for retrieving the data from my external application/database ? (the data retrieved from the application is JSON-formatted) thanks for any advice / help!

Upvotes: 0

Views: 1439

Answers (2)

Aristeidis Karavas
Aristeidis Karavas

Reputation: 1956

Lets take a simple list -> detail view and make it as simple as possible in order to get some results. Then you can add some helper classes and include them via namespaces etc.

ListAction (yourExtension/Classes/Controller/EventController)

/**
* action list
*
* @return void
*/
 public function listAction()
 {
      $events =  (some function which gets all the available events and results to a json output)
      $decodedEvents = json_decode($events , true);

      (in case you need to see what you got back)
      \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($decodedEvents);

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

List HTML (yourExtension/Resources/Private/Templates/Event/List)

<f:for each="{events}" as="event">
      <h2>{event.title}</h2>
</for>

Now if i am not wrong, every event has a uid or something that identifies it. So on the same template you can do the following:

<f:for each="{events}" as="event">
      <h2>{event.title}</h2>
      <f:link.action action="detail" arguments="{eventId: event.uid}">More</f:link.action>
</f:for>

What this does, is to generate a link which links to your detail action with an extra parameter, the eventId.

Bedore TYPO3 renders the detail page it will go through your detail action to get the information for this specific event in order to display them.

DetailAction (yourExtension/Classes/Controller/EventController)

/**
* action detail
*
* @return void
*/
 public function detailAction()
 {
      $args = $this-request->getArguments();
      $eventId = $args['eventId'];

      $getEvent = (some function that gets a specific id and results to a json output)
      $decodedEvent = json_decode($getEvent , true);

      (in case you need to see what you got back)
      \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($decodedEvent );

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

What this does, it to retrieve the argument that we specified in order to get the identifier of the event and include it on your api request to get the event back. Then we just just decode it to a normal array and we send the results to the FrontEnd.

Detail HTML (yourExtension/Resources/Private/Templates/Event/Detail)

<h2>{event.title}</h2>

Thats a simple list -> detail proccess on TYPO3. You will oft see that the detail view as show but is basically the same thing.

You can replace the variables with a dummy json values and play around. I tested the code while i was writing this answer and it works.

Classes/Service

As for the Classes/Service that mentioned by Rudy Gnodde, you can put your library in there (assuming that you already have coded some functions like getEvent, getAllEvents, getPersons etc.) and then call them in the controller.

use \Vendor\YourExtension\Service\RestApiClass;

/**
* action list
*
* @return void
*/
 public function listAction()
 {
      $events =  RestApiClass::getAllEvents();
      $decodedEvents = json_decode($events , true);

      (in case you need to see what you got back)
      \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($decodedEvents);

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

/**
* action detail
*
* @return void
*/
 public function detailAction()
 {
      $args = $this-request->getArguments();
      $eventId = $args['eventId'];

      $getEvent = RestApiClass::getEvents($eventId);
      $decodedEvent = json_decode($getEvent , true);

      (in case you need to see what you got back)
      \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($decodedEvent );

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

If you need more information feel free to ask.

Best regards

Upvotes: 0

Rudy Gnodde
Rudy Gnodde

Reputation: 4565

What we usually do is create a Service (in Classes/Service) and use that to connect to the webservice and fetch the data. If you want Models, you can create them there as well.

However, now that I think about it, technically it should be a Repository. It shouldn't matter where the Repository gets its data. Extbase shouldn't have any problem with a completely custom Repository (not extending any other class).

Upvotes: 1

Related Questions