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

Generating urls for \Phalcon\Mvc\Micro\Collection

I'm looking for a solution that will allow me to generate urls using Phalcon\Mvc\Url for \Phalcon\Mvc\Micro\Collection (using a controller as a handler).

This is my collection setup

$app = new \Phalcon\Mvc\Micro($di);  //$di contains my url service

$nappiCollection = new \Phalcon\Mvc\Micro\Collection();

$nappiCollection

    ->setPrefix('/v1/nappi')
    ->setHandler('\PhalconRest\Controllers\NappiController')
    ->setLazy(true);

$nappiCollection->get('/{id:[0-9]+}', 'show');

$app->mount($nappiCollection);

I basically want to be able to do this:

     $nappiCollection->get('/{id:[0-9]+}', 'show')->setName('show-nappi')

and then generate a url like this:

    $app->url->get(array(
        'for' => 'show-nappi',
    )),

unfortunately \Phalcon\Mvc\Micro\Collection does not have a setName method. How should I deal with this?



98.9k
Accepted
answer

You can pass the name as third parameter:

$nappiCollection->get('/{id:[0-9]+}', 'show', 'show-nappi');


778
edited Nov '14

Fantastic exactly what I'm looking for! Thankyou!