Problem with using multiple colon separated values in URL

Hello Phalcon community,

I have problem with routing and generating URLs who have more than one value separated by colon. I found one discusion here about that (http://forum.phalconphp.com/discussion/2746/use-colon-as-seperator-in-url) but that was about one value separated by colon.

I'm trying to reach something like this:

So I created model, views and controller for that, and in routes.php I added something like this:

...
$router->add('/movies', "Movies::allMovies")->setName('movies-index');
$router->add('/movies/page:([0-9]+)', array(
    'controller' => 'movies',
    'action'     => 'allMovies',
    'page'       => 1
))->setName('movies-index-page');
$router->add('/movies/page:([0-9]+)/sort:([a-z]+)', array(
    'controller' => 'movies',
    'action'     => 'allMovies',
    'page'       => 1,
    'sort'       => 2
))->setName('movies-index-page-sort');
...

In controller I'm receiving this data via dispatcher and it works fine. Problem is starting when I'm trying to generate URL via {{ url(["for"]) }} or {{ link_to(["for"]) }}. For router named "movies-index" everything works fine, and url function generates me a proper URL address, same thing happens for movies-index-page ({{ url(["for": "movies-index-page", "page": ":10"]) }}). I have a problem when I'm trying generate URL with second parameter separated by semicolon. When i write something like this:

{{ url(["for": "movies-index-page-sort", "page": ":12", "sort": ":created"]) }}

URL function generates me something like this:

/movies/page:12:created/sort

I have tried everything. I even tried solving that problem with documentation (http://docs.phalconphp.com/en/latest/reference/dispatching.html#preparing-parameters) but without any positive result :(

II'll be grateful for any hint to solve this problem :)

P.S. Sorry for my rusty English. It isn't my native language but I'm trying as best I can :)



27.4k

Hi,

taken from the doc you have noticed:

"If the desired schema is: http://example.com/controller/key1:value1/key2:value, the following code is required:"

<?php

use Phalcon\Dispatcher,
    Phalcon\Mvc\Dispatcher as MvcDispatcher,
    Phalcon\Events\Manager as EventsManager;

$di->set('dispatcher', function() {

    //Create an EventsManager
    $eventsManager = new EventsManager();

    //Attach a listener
    $eventsManager->attach("dispatch:beforeDispatchLoop", function($event, $dispatcher) {

        $keyParams = array();
        $params = $dispatcher->getParams();

        //Explode each parameter as key,value pairs
        foreach ($params as $number => $value) {
            $parts = explode(':', $value);
            $keyParams[$parts[0]] = $parts[1];
        }

        //Override parameters
        $dispatcher->setParams($keyParams);
    });

    $dispatcher = new MvcDispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});