May 16 2009

As with everything I do, I hack together solutions when there’s probably a much better and easier way of doing things. But anyway – if there’s not an easier way, then here’s a solution to my problem, and if there is, well… I’ll just use that in future…

My problem is that I want pretty URLs for my app, such as http://blah.com/home/folder/5/item/27. The URL structure of Zend Framework means that the URL has to look like this: http://blah.com/home/index/folder/5/item/27. There’s probably a way around that (my original idea was to use http://blah.com/home/?folder=5&item=27 but that’s just ugly), but I’ll settle for using the pointless verb “go” as the action name, resulting in http://blah.com/home/go/folder/5/item/27. But I also want that goAction to be called when I just go to http://blah.com/home/. I had a quick look at the ZF documentation, but I couldn’t see much about setting a default action in a controller, so here’s my solution:

class HomeController extends Zend_Controller_Action
{
	public function indexAction()
	{
		// ... action code here ...
	}
 
	public function goAction()
	{
		$this->_forward('index');
	}
}

As I said, there’s probably an easier way around this, but this works for me, so ner!

Update: As always, there is an easier way of doing this and I was reading the wrong part of the documentation. As Patrick says in his comment:

Hello, there is a way to specify default module, controller and action for each active route in the Zend_Controller_Router used by Zend_Controller_Front.

http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard.variable-defaults

just pass an array

One Response to “Zend Framework: Default Action in a Controller”

  1. Patrick Barroca says:

    Hello, there is a way to specify default module, controller and action for each active route in the Zend_Controller_Router used by Zend_Controller_Front.

    http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard.variable-defaults

    just pass an array

Leave a Reply