We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Can't route same url with different HTTP methods in multi module with router groups

Hello!

As the title says, I am building a multi-module application in Phalcon with two modules, one for the backend (a REST-ful api) and one for the frontend. The frontend part is simply serving a SPA in backbone for the moment, but the backend will do a bit more, handling data-access and business logic etc.

I am using a router group like so:

class TownRoutes extends \Phalcon\Mvc\Router\Group
{
    public function initialize()
    {
        // Default paths
        $this->setPaths(array(
            'module' => 'api',
            'controller' => 'town',
            'namespace' => 'Pangea\Api\Controllers'
        ));

        // All the routes start with /town
        $this->setPrefix('/api/towns');

        // Routes
        $this->add('', array('action' => 'index'));

        // CRUD
        $this->addGet('/:int', array('action' => 'details', 'id' => 1));
        $this->addPost('/:int', array('action' => 'create', 'id' => 1));
        $this->addPut('/:int', array('action' => 'update', 'id' => 1));
        $this->addDelete('/:int', array('action' => 'delete', 'id' => 1));
    }
}

So in the frontend part, when running locally, I will call it like: localhost:8000/api/towns/{id} and depending on HTTP post, I want different actions in the TownController to be invoked. Unfortunatley, only GET action gets invoked.

Here's the controller:

class TownController extends ControllerBase
{
    public function detailsAction($id)
    {
        // Stuff
    }

    public function deleteAction($id)
    {
        // Stuff
    }

    public function createAction($id)
    {
        // Stuff
    }

    public function updateAction($id)
    {
        // Stuff
    }
}

So, anyone has any idea why no other HTTP method than GET will work? (the others return a 400).

Ofcourse, I could route them to the same action like add('/:int', array('action' => 'CRUD', 'id' => 1)); and then in the action check what HTTP method was used and redirect accordingly, but that feels lika a hack, and I would not want to do that if possible.

Thanks in advance!



920
Accepted
answer

Hi again!

Forget about this question, I was missunderstanding some concepts and the routing was working all along =)

Thanks anyway.