Thanks for your advice and the link Daniel.  I'll take a look sounds like a 
good option.  To save the controller also having to do a similar DB query to 
pull the entity back, I'm guessing that I could store it in a session variable 
from within the preDispatch.  I'm also using Doctrine, so another alternative 
would be to just query again and allow the doctrine entity cache to take care 
of this 'caching' element.

Cheers,  

Greg

On Friday, 22 May 2015 at 14:20, Daniel Latter wrote:

> Hi Greg,
>  
> You could register a preDispatch event in your module's module.php.
>  
> For example, we use a preDispatch event to check if a user is logged in when 
> accessing certain pages.
>  
> You could easily check for a valid "TopEvent" from inside your preDispatch 
> method, and redirect accordingly.
>  
> For example:  
>  
> https://gist.github.com/rettal/4a6e9ae973ed09fae382
>  
> The example above checks if logged in, but can easily be adapted to suit your 
> needs I think,
>  
> Daniel.
>  
>  
>  
>  
> On 22 May 2015 at 13:35, Greg Frith <gfr...@gmail.com 
> (mailto:gfr...@gmail.com)> wrote:
> > Hi all,
> >  
> > I'm developing a ZF2 site which has a booking process for events.  The 
> > booking process part of the site has consistent URLs like:
> >  
> > http://mysite.com/book/TopEvent/availability
> > http://mysite.com/book/TopEvent/seats
> > http://mysite.com/book/TopEvent/payment
> > http://mysite.com/book/TopEvent/confirm
> >  
> > I have an event entity stored in a relational database, that amongst other 
> > things has a 'url_name' field which is used to reference the event in urls, 
> > a bit nicer than using plain old ids.  So in the above URL examples, the 
> > 'url_name' is TopEvent.
> >  
> > I have a controller names BookingController which has all the actions for 
> > the above sample URLs, for example seatsAction.  Currently, at the start of 
> > each action I'm having to pull the 'url_name' parameter from the params, 
> > and search for the event by url_name in my database.  If no event is found, 
> > I return 404.  I have a controller plugin to help with this, and the code 
> > looks something like:
> >  
> > public function seatsAction()
> > {
> > // Check we have valid event
> > if (!$event = $this->SIL()->getEventFromRoute()) {
> > $this->logger->err("On seats page with no valid event, returning 404");
> >         $this->getResponse()->setStatusCode(404);
> >         return;
> > }
> > ….
> >  
> >  
> > If I have 8 actions in my booking controller, this gets a bit repetitive.  
> > Essentially, I'd like a way to return a 404 on all routes/action where the 
> > event cannot be found.
> >  
> > And finally to the crux of my mail, I'd like some advice on the best way to 
> > do this?
> >  
> > Perhaps I should be listening for some event within my controller 
> > (dispatch?) and doing my check here (can I return 404 from this).
> >  
> > Or should I override the parent controllers onDispatch event?
> >  
> > Or, could I take this further and create a custom router that validates 
> > this part of the URL against the database?  If I went down this route, 
> > could that custom router also load the entity for use within that request?
> >  
> > Any advise greatly appreciated.
> >  
> > Cheers,
> > Greg.
> >  
> > :wq

Reply via email to