Quantcast

Router Problems...

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Router Problems...

travis
I've been working along fine with some basic routers and have stumbled across a problem...which is why I'm here.  ;-)

I want to try to point my browser to either of the following:
 - http://mydomain.com/events
 - http://mydomain.com/events/?page-number=2

(The reason I'm doing the latter is because I'm using AJAX to move back and forth through a table of events without reloading the whole page.  The jQuery history plugin that I'm using, https://github.com/balupton/History.js, handles this for me and constructs it this way.)

Anyway, here is the pertinent part of my configuration:
            'events' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route' => '/events[/\?page-number=:pageNumber]',
                    'defaults' => array(
                        'controller' => 'events',
                        'action'     => 'index',
                        'pageNumber'   => 1,
                    ),
                    'constraints' => array(
                        'page' => '\d+',
                    ),
                ),
            ),

That was what I was TRYING to do.  But, of course, it doesn't work.  It works for the first request (from above), but not the second.

I have tried without the question mark and have manually entered the request and it works fine, but any attempt at trying to get the Segment router to match on a ? has failed.  I presumed it would have to be escaped with a backslash, as I've done, but that doesn't seem to do the trick.

So...does anyone see what I'm doing wrong with the route definition?

NOTE: Please...I am aware of the fact that there are a multitude of opinions about how "ajaxy" sites should work with regards to the browser's history.  I'm aware of some of the alternatives and workarounds, but that's not what I'm (primarily) interested in discussing.  I'm using this particular jQuery plugin and this is what I'm currently stuck on, so any help in this regard will be much appreciated.  :-)

Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Router Problems...

Jurian Sluiman-3
2012/6/4 travis <[hidden email]>

> I've been working along fine with some basic routers and have stumbled
> across
> a problem...which is why I'm here.  ;-)
>
> I want to try to point my browser to either of the following:
>  - http://mydomain.com/events
>  - http://mydomain.com/events/?page-number=2
>
> (The reason I'm doing the latter is because I'm using AJAX to move back and
> forth through a table of events without reloading the whole page.  The
> jQuery history plugin that I'm using,
> https://github.com/balupton/History.js, handles this for me and constructs
> it this way.)
>
> Anyway, here is the pertinent part of my configuration:
>            'events' => array(
>                'type'    => 'Segment',
>                'options' => array(
>                    'route' => '/events[/\?page-number=:pageNumber]',
>                    'defaults' => array(
>                        'controller' => 'events',
>                        'action'     => 'index',
>                        'pageNumber'   => 1,
>                    ),
>                    'constraints' => array(
>                        'page' => '\d+',
>                    ),
>                ),
>            ),
>
> That was what I was TRYING to do.  But, of course, it doesn't work.  It
> works for the first request (from above), but not the second.
>
> I have tried without the question mark and have manually entered the
> request
> and it works fine, but any attempt at trying to get the Segment router to
> match on a ? has failed.  I presumed it would have to be escaped with a
> backslash, as I've done, but that doesn't seem to do the trick.
>
> So...does anyone see what I'm doing wrong with the route definition?
>
> NOTE: Please...I am aware of the fact that there are a multitude of
> opinions
> about how "ajaxy" sites should work with regards to the browser's history.
> I'm aware of some of the alternatives and workarounds, but that's not what
> I'm (primarily) interested in discussing.  I'm using this particular jQuery
> plugin and this is what I'm currently stuck on, so any help in this regard
> will be much appreciated.  :-)
>
> Thanks.
>

If you have the url /events?pageNumer=10 then the /events part is managed
by the router, the ?pageNumber=10 is just an additional query parameter.
You have two solutions:

1. Use pageNumber as router parameter. You could format your route like
/events/:pageNumber resulting in a url like /events/10. I am not sure if
you explicitly avoiding this because of your "ajaxy" concern.

2. Use /events as route and handle the query parameter yourself. You should
keep track of it and build the query string yourself.
--
Jurian Sluiman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Router Problems...

travis
Thanks for the reply.  Please see my comments inline...

Jurian Sluiman-3 wrote
2012/6/4 travis <[hidden email]>

If you have the url /events?pageNumer=10 then the /events part is managed
by the router, the ?pageNumber=10 is just an additional query parameter.
You have two solutions:

1. Use pageNumber as router parameter. You could format your route like
/events/:pageNumber resulting in a url like /events/10. I am not sure if
you explicitly avoiding this because of your "ajaxy" concern.
Yes, I'm aware of this and would write my router this way if I didn't need some sort of a solution to solve the ajax paging and browser history problem.  The jQuery plugin that I'm using, https://github.com/balupton/History.js, builds the queries this way, for better or worse, and so I have to deal with it.  If there's some other solution that you or anyone else knows that would allow me to have a url like /events/10, I'm definitely open to it.  Just let me know.  :-)

Jurian Sluiman-3 wrote
2. Use /events as route and handle the query parameter yourself. You should
keep track of it and build the query string yourself.
--
Jurian Sluiman
I'm not sure that I follow your second suggestion.  When you say, "handle the query parameter yourself", do you mean on the PHP or the JS end of things?  Or both, perhaps...?  And, "keep track of it and build the query string yourself"...?  Do you mean that I should not rely on the jQuery history plugin?  If that's what you're suggesting, you may be right that I could get the solution that I want...but I'm a php guy and, unfortunately, my JS is weak, so I have to use the solutions that people have already made.

Btw, the jQuery history plugin is similar to YUI's history "utility": http://yuilibrary.com/yui/docs/history/.  It looks like it has a bit more flexibility with regards to what the URL will be.  The problem is that I'm already using jQuery (mainly because of Bootstrap), so I don't want to have to bring in something else just for one piece.

Anyway, I'll keep digging around.  In the meantime, if anyone knows how to write the route so that it accepts either http://mydomain.com/events or http://mydomain.com/events/?page-number=2, I'd very much appreciate it.

Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Router Problems...

Jurian Sluiman-3
2012/6/6 travis <[hidden email]>

> Jurian Sluiman-3 wrote
> >
> > 2. Use /events as route and handle the query parameter yourself. You
> > should
> > keep track of it and build the query string yourself.
> > --
> > Jurian Sluiman
> >
>
> I'm not sure that I follow your second suggestion.  When you say, "handle
> the query parameter yourself", do you mean on the PHP or the JS end of
> things?  Or both, perhaps...?  And, "keep track of it and build the query
> string yourself"...?  Do you mean that I should not rely on the jQuery
> history plugin?  If that's what you're suggesting, you may be right that I
> could get the solution that I want...but I'm a php guy and, unfortunately,
> my JS is weak, so I have to use the solutions that people have already
> made.
>

You use a route /events and do not include the query parameter for the
route. Inside your controller you grab the query parameter yourself (you
have access to the Request in your controller by $this->getRequest()) and
in your view you append the query parameter to your /events route. Then you
skip the routing part for that parameter and track the value of pageNumber
yourself.
--
Jurian Sluiman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Router Problems...

travis
Thanks for your explanation.

I don't know if I mentioned it, but I'm using the latest beta version of ZF2, and I am not 100% certain how to get around explicitly defining routes.

I may have found a way to change the URL that the jQuery history plugin that I'm using sets in the browser, so I may have avoided having to deal with the issue of the question mark in the route definition...for now.  I'm still interested in hearing from someone how I can use a Segment route and have an optional section contain a question mark.  I'm guessing it may come up at one point in time or another....

Thanks.
Loading...