0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Looks quite promising. I'll try it on Monday and let you know.
It doesn't work. I copied the routes and /companies throws 404. Mind if you try it out, please?
Hi Ali, I did not follow your Controller & Action names. Did you update these when you copied this config? A 404 based upon config usually denotes that a route was found (e.g. /companies) but that the corresponding Controller::nameAction was not. Possibly just the view not matching the action, but that usually resolves to a view not found error type.
I actually did. I also left out the child routes and tried single route with index action, still 404
What is the name of your Controller? And does the use statement match the folder structure (per PSR-4) ? And what is the action name? And does all the previous match this config? (If not, can you supply the differences?)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.