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

What is wrong with this route prefix

Hi ! I am using annotations to define routes. But i have troubles with a route for a subresource.

I need an endpoint to manage the users addresses in my REST API, so i defined the next resource:

<?php
use Phalcon\Mvc\Router\Annotations as RouterAnnotations;
/* More  ... */
$router = new RouterAnnotations(false);
$router->addResource("Addresses", "/users/{idUser:[0-9]+}/addresses");
/* More ... */

And in my AddressesController:

<?php

use Phalcon\Mvc\Controller;
use Services\Customers as CustomersService;

/**
 * @RoutePrefix("/users/{idUser:[0-9]+}/addresses")
 */
class AddressesController extends Controller {

    /**
     * @Get("/{idAddress}")
     * @param  int  $idUser    Id del usuario.
     * @param  int $isAddress Id del domicilio.
     */
    public function readAction($idUser, $idAddress) {
        var_dump($idUser, $idAddress);
    }
}

So, when i do a GET requesto to /users/10/addresses/2 Phalcon dispatch to 404 page. Any ideas ?

edited Aug '16

Im not sure if routeprefixes can even accept such a syntax like having parameter or regex. can you just check for instance if changing:

/users/{idUser:[0-9]+}/addresses to /users/10/addresses

And accessing your url again works ?

Im not sure if routeprefixes can even accept such a syntax like having parameter or regex. can you just check for instance if changing:

/users/{idUser:[0-9]+}/addresses to /users/10/addresses

And accessing your url again works ?

Yes, harcoding the user id, the url works



145.0k
Accepted
answer

So the issue is what i wrote. Create issue on github maybe, someday maybe this will be added. You can /users as prefix for now, sorry for this.

edited Aug '16

You can always override this behaviour.

This is another example (Micro app).

class MicroCollection extends \Phalcon\Mvc\Micro\Collection
{
    function setHandler($handler = null, $lazy = 0x0a)
    {
        //Inject current namespace and normally proceed further treatment of request to a parent method
        return parent::{__FUNCTION__}(__NAMESPACE__ . '\\' . $handler, $lazy);
    }
}