Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



85.3k
Accepted
answer

You can pass the name as third parameter:

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


513
edited Nov '14

Fantastic exactly what I'm looking for! Thankyou!