0

I have a website that stores inventory and I need to make a REST API because I have to put the web component and deliver the data to it. Communication will be secured by JWT. I found a very simple solution zf3-rest-api but I can't implement it because I have some strange file structure (I don't have modules.config.php etc.) I'm afraid it's not ZF3 or even ZF2. I can write a custom solution but I don't know where should I put the code (sorry I am a front-end developer)? in modules? and how to deal with routing? so that I can refer to it via http://example.com/api/?

enter image description here

JanuszFrontEnd'u
  • 451
  • 6
  • 14
  • This module has the following requirements: Zend Framework 3 or greater. PHP 7 or greater. You can install this module into your Zend Framework application using composer. The recommended way to install composer packages is: composer require scorpjio/zf3-rest-api – bujals Aug 21 '20 at 12:31

1 Answers1

1

This is a ZF1 application tree and ZF1 has its REST implementation.

You can get here an example of controller that extends Zend_Rest_Controller. Let's say you call it "MyRestfulController".

then you must register your rest routes, you can do it in your Bootstrap.php

protected function _initRestRoute()
{
    $frontController = $this->bootstrap('frontController')->getResource("frontController");
    $restRouteUL = new Zend_Rest_Route($frontController, array(), [
        'default' => [
            'my-restful'
        ]
    ]);
    $frontController->getRouter()->addRoute('rest', $restRouteUL);
}

OR

If you do not need rest but just an API that return some JSON you may skip the Restful part and disable layout in you controller ( so not extending Zend_Rest_Controller ), overriding the "init()" method

    public function init()
{
    parent::init();
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    $this->getResponse()->setHeader("Content-type", "text/json");
    
    /**
     * This is important for the helper not to exit the dispatch loop
     */
    $this->_helper->json->suppressExit = true;
}

Then in your action

public function getMyDataAction()
{
    $data = [];

    // your filters and all the logic to populate $data

    $this->_helper->json($data);
}

Keep in mind that ZF1 has several performances issues, mainly related to the resource configuration that should be replaced by serviceManager as much as possible, and also by Zend_Date.

Sergio Rinaudo
  • 2,303
  • 15
  • 20
  • Thank you very much for your answer, but where should I create this restapi? should I create a new folder in the `library`? I would like each controller to return different data, e.g. `getBooksAction()`, `addAuthorBookAction()` etc. – JanuszFrontEnd'u Aug 22 '20 at 09:10
  • I would advice you to create a controller with all the related actions, for example BookController.php with all books related action. Also I would use the second method described in my response, forget about REST and make an API to render JSON responses. To keep everything as much simple as possible, just create your controllers in the default module: copy/paste the init method as described. – Sergio Rinaudo Aug 24 '20 at 08:50
  • ZF1 default route will automatically register your controller, so just call yourapp.com/book/add-author or yourapp.com/book/get. Just try to get some empty responses to see if everything works. As last advice I recommend not to put any business logic in your controller, in ZF1 they are not reusable, so create different components that make one task and call them in your action ( possibly as a service using a service manager implementation )) – Sergio Rinaudo Aug 24 '20 at 09:15
  • cool! I just need to create a service (for jwt logic) that will be standalone, but I see that it is not so easy to refer to the service I have to use 'new' and unfortunately then I create a new instance... :/ but I want to work globally on one and store the token value. – JanuszFrontEnd'u Aug 25 '20 at 14:08