Should I use AbstractRestfulController for ajax call within zend-mvc (zf3)?

I am looking for the best practices. Where can I find a good example of zf3 working with ajax call?

In the documentation of zend-view you will find the topic: ā€œCreating and Registering Alternate Rendering and Response Strategiesā€

For a JSON response you need to register the associated response strategy (JsonStrategy):

namespace Application;

use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    /**
     * Returns configuration to merge with application configuration
     *
     * @return array
     */
    public function getConfig()
    {
        return [
            /* ... */
            'view_manager' => [
                /* ... */
                'strategies' => [
                    'ViewJsonStrategy',
                ],
            ],
        ];
    }
}

And then you can use the view-model for JSON in your controller:

namespace Application;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\JsonModel;

class MyController extends AbstractActionController
{
    /**
     * Lists the items as JSON
     */
    public function listJsonAction()
    {
        $items = /* ... get items ... */;
        $viewModel = new JsonModel();
        $viewModel->setVariable('items', $items);
        return $viewModel;
    }
}

This will give you a correct response with all needed headers.

(All code examples are taken from the documentation.)


The AbstractRestfulController can be used for a RESTful implementation but it is not needed for a simple JSON response.

1 Like

Within admin panel I want to do some actions with user data: create, list, update, delete with vuejs. All this actions will be executed via ajax. Also, I want to do such actions with another entities (news, articles, blog posts etc.). Is it still appropriate to use simple JSON response? What about https://github.com/zfcampus/zf-rest ?

Thank you for your response.

If you follow the REST architectural style then you can use AbstractRestfulController from zend-mvc or zf-rest. Otherwise use the JsonStrategy and the JsonModel of zend-view.

1 Like

Frank, can you tell me few words about apigility ( https://github.com/zfcampus/zf-apigility ) ? Can I use it with zf3 with my current zend-mvc project (or with ZendSkeletonApplication ( https://github.com/zendframework/ZendSkeletonApplication ) )? Is it required to use admin UI panel (zf-apigility-admin-ui) ?

Should I asked a new question about apigility at this forum?

Maybe Iā€™m wrong, but I have the impression that you are looking for tools before you have defined a goal.


If you have experience with REST then you can use the AbstractRestfulController without much effort in your current zend-mvc application. Give it a try for one of your different sections.

I have a goal. I want to rewrite my old project from ZF1 to ZF3. It uses a lot of ajax calls and it seems that calls to controllers as I have now is a bit messy. That is why I want to put in order that ajax calls. That is why I am trying to choose the best way which I will stick to over the next few years.

When you are still at the beginning then use zend-expressive.

Is zend-expressive scalable for a large project?

Yep. As your scope grows, you mostly likely just to split sub-routes into multiple pipes, but it can easily grow to hundreds of endpoints.

Does expressive have modularity as zend-mvc 3?

The documentation of zend-expressive contains a section for this: ā€œModular applicationsā€

2 Likes