V
V
Vadim Ivanenko2015-03-02 14:01:34
Zend Framework
Vadim Ivanenko, 2015-03-02 14:01:34

How to change BjyAuthorize behavior to work with REST API (HTTP methods, RestfulController)?

You need to make it work with RestfulController's actions (get, getList) or with HTTP methods coming in Request (GET, POST, PUT) instead of AbstractActionController's actions.
At the same time, it is desirable to retain the ability to control and ordinary actions, since they can be created in RestfulController'e. It would probably be more convenient to get the names of the RestfulController'a methods (get, getList), and not HTTP Request'a, so as not to mess up the config add. http_method/controller_method parameter.
How can I get the name of the RestfulController's called action (get, getList, etc.)?
What services and providers need to be changed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Mokhov, 2015-03-06
@mokhovcom

config/module.config.php

return [
    'router' => [
        'routers' => [
            'api-login' => [ // REST
                'type' => 'Literal',
                'options' => [
                    'route' => '/api/login',
                    'defaults' => [
                        'controller' => 'YourModule\Controller\Login'
                    ],
                ],
            ],
            'login' => [
                'type' => 'Literal',
                'options' => [
                    'route' => '/login',
                    'defaults' => [
                        'controller' => 'YourModule\Controller\Login'
                        'action' => 'login',
                    ],
                ],
            ],
        ],
    ],
    'view_manager' => [
        'strategies' => [
            'ViewJsonStrategy',
        ],
    ],
    'controllers' => [
        'invokables' => [
            'YourModule\Controller\Login' => 'YourModule\Controller\LoginController',
        ],
    ],
];

src/YourModule/Controller/LoginController.php
class LoginController extends AbstractRestfulController {
    public function create($data) {
        // POST данные пришедшие по REST
        return new JsonModel(['status' => 'error', 'message' => 'Логин и/или пароль указан не верно.']);
    }

    public function getList() {
        // GET данные без id
        return new JsonModel(['item1', 'item2', 'item3']);
    }

    public function loginAction() {
        // обычный Action
        return new ViewModel();
    }
}

you should pay attention to the fact that:
  • Controller inherited from AbstractRestfulController
  • The config contains the strategy ViewJsonStrategy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question