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
