Ali Rasheed
Ali Rasheed

Reputation: 2817

Making restful requests in zend framework 3

So I am trying to make restful web request but, I may have configured it wrong which I cannot seem to fix.

I have mind that.

/companies      GET      -> index method
/companies      POST     -> add post method
/companies/1    GET      -> show method
/companies/1    POST     -> edit post method

Here is what I have tried

"router"                             => [
    "routes"                         => [
        "companies"                  => [
            "type"                   => "segment",
            "options"                => [
                "route"              => "/companies[/:id]",
                "constraints"        => [
                    "id"     => "[0-9]*",
                ],
                "defaults"           => [
                    "controller"     => Controller\CompaniesController::class,
                    "action"         => "index",
                ],
            ],
            "may_terminate"          => true,
            "child_routes"           => [
                "companiesIndex"     => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "GET",
                        "route"      => "/companies",
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "index"
                        ],
                    ],
                ],
                "companiesAddPost"   => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "POST",
                        "route"      => "/companies",
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "add"
                        ],
                    ],
                ],
                "companiesShow"  => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "GET",
                        "route"      => "/companies/:id",
                        "constraints"        => [
                            "id"     => "[0-9]*",
                        ],
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "show"
                        ],
                    ],
                ],
                "companiesEditPost"  => [
                    "type"           => "segment",
                    "options"        => [
                        "verb"       => "PATCH",
                        "route"      => "/companies/:id",
                        "constraints"        => [
                            "id"     => "[0-9]*",
                        ],
                        "defaults"   => [
                            "controller"                 => Controller\CompaniesController::class,
                            "action"                     => "edit"
                        ],
                    ],
                ],
            ],
        ],

/companies index method works. Not sure about Post. But whenever I try to request /companies/1 it still shows index method. What is wrong and how should I fix it.

Upvotes: 0

Views: 58

Answers (1)

rkeet
rkeet

Reputation: 3468

You have double declared routes. You have declared route /companies[/:id] and given it child_routes: /companies. In essence, you've got: /companies, /companies/:id and /companies/companies. Also, you're using segment routes. For Rest routes you should use the Method routes.

For example:

<?php

namespace Company;

use Company\Controller\Company\AddController;
use Company\Controller\Company\DeleteController;
use Company\Controller\Company\EditController;
use Company\Controller\Company\IndexController;
use Company\Controller\Company\ViewController;
use Zend\Router\Http\Method;

return [
    'router' => [
        'routes' => [
            'companies_index' => [
                'type'          => Method::class,
                'may_terminate' => true,
                'options'       => [
                    'verb'     => 'GET',
                    'route'    => '/companies',
                    'defaults' => [
                        'controller' => IndexController::class,
                        'action'     => 'index',
                    ],
                ],
                'child_routes'  => [
                    'companies_view'   => [
                        'type'          => Method::class,
                        'may_terminate' => true,
                        'options'       => [
                            'verb'     => 'GET',
                            'route'    => '/:id',
                            'defaults' => [
                                'controller' => ViewController::class,
                                'action'     => 'view',
                            ],
                        ],
                    ],
                    'companies_edit'   => [
                        'type'          => Method::class,
                        'may_terminate' => true,
                        'options'       => [
                            'verb'     => 'PATCH',
                            'route'    => '/:id',
                            'defaults' => [
                                'controller' => EditController::class,
                                'action'     => 'edit',
                            ],
                        ],
                    ],
                    'companies_delete' => [
                        'type'          => Method::class,
                        'may_terminate' => true,
                        'options'       => [
                            'verb'     => 'DELETE',
                            'route'    => '/:id',
                            'defaults' => [
                                'controller' => DeleteController::class,
                                'action'     => 'delete',
                            ],
                        ],
                    ],
                ],
            ],
            'companies_add'   => [
                'type'          => Method::class,
                'may_terminate' => true,
                'options'       => [
                    'verb'     => 'POST',
                    'route'    => '/companies',
                    'defaults' => [
                        'controller' => AddController::class,
                        'action'     => 'add',
                    ],
                ],
            ],
        ],
    ],
];

Also this sub-question:

But whenever I try to request /companies/1 it still shows index method.

That's because your initial index route is /companies[/:id]. So if you add /1 to the URL your requesting, that route will still match and send you to the index action of the CompaniesController which you have configured there.

Upvotes: 1

Related Questions