franmomu / silex-pagerfanta-provider

Pagerfanta Silex Provider
14 stars 11 forks source link

Custom routename and route params by controller #2

Closed jmontoyaa closed 11 years ago

jmontoyaa commented 11 years ago

Sometimes you need to change the route name and route params. You can't override the original one's because of Pimple:

'routeName' => null, 'routeParams' => array()

franmomu commented 11 years ago

Thanks for taking the time to send this PR! I'm not really sure what you mean about override the original one's because of the Pimple. When you register the provider you can do this:

$app->register(new PagerfantaServiceProvider(), array(
    'pagerfanta.view.options'  => array(
        'routeName'   => 'my_route',
        'routeParams' => array('parameter1' => 'value1')
    )
));

And this options are merged in the provider's boot method. Anyway reviewing this code, I think it doesn't make sense to configure this parameters when you register the provider because it will be different in every pagination, like WhiteOctoberPagerfanta does. What do you think?

jmontoyaa commented 11 years ago

Hello! Yes I agree with your comment but... I'm using controller as services and I need to override those parameters because I use it in different actions.

In my bootstrap file I have this:

$app->register(new PagerfantaServiceProvider(), array(
    'pagerfanta.view.options'  => array(
        'routeName'   =>nul,
        'routeParams' => null
    )
));

For example I have a controller with 2 actions but I need to generate 2 URLs like this:

class MyController
{
    private $app;

    public function paginationAction($page) {

        $adapter = new FixedAdapter($nbResults, array());
        $pagerfanta = new Pagerfanta($adapter);
        $pagerfanta->setMaxPerPage($this->maxPerPage);
        $pagerfanta->setCurrentPage($page); // 1 by default
        $this->app['pagerfanta.view.router.name'] = 'route1'; // <<- different route
        $this->app['pagerfanta.view.router.params'] = array(
            'filter' => '1',
            'page' => $page
        );
    }

    public function paginationDifAction($page) {
        $adapter = new FixedAdapter($nbResults, array());
        $pagerfanta = new Pagerfanta($adapter);
        $pagerfanta->setMaxPerPage($this->maxPerPage); 
        $pagerfanta->setCurrentPage($page); // 1 by default
        $this->app['pagerfanta.view.router.name'] = 'route2';  //  << different route
        $this->app['pagerfanta.view.router.params'] = array(
            'filter' => '2',
            'page' => $page
        );
    }
}

And I can't override the original routeName and routeParams like this in every action ...

 $this->app['pagerfanta.view.options']['routeName'] = 'something';
 $this->app['pagerfanta.view.options']['routeParams'] = array();
franmomu commented 11 years ago

Perfect, I'll merge and then I'll remove routeNameand routeParams in the Provider registration.

One more thing, why use this version for propertyaccess:

"symfony/property-access": ">=2.2,<2.4-dev"

I mean, it is not better to rely on the 2.2 stable?

Thanks!

jmontoyaa commented 11 years ago

yes, I just change the version, sorry!

franmomu commented 11 years ago

Thanks!