views:

988

answers:

1

I've just put together a very basic site using the Zend Framework and its MVC. (Actually, I'm not even using models at the moment, it's all just Controllers/Views for static info so far).

When I started toying with the Forms I realized that in the examples for Zend_Form they use something like this this set the form's action:

$form->setAction('/user/login')

Which contains the URL. I understand that Zend Framework has Routes and that they can be named, but I can't seem to grasp from the manual how to create a simple route for certain Controller/Actions so that I could do something like this:

$form->setAction($named_route)
// or 
$form->setAction('named_route')

I hope my question is clear. I wasn't able to locate any duplicate questions, but if you spot one and point it out I won't mind.

Links to resources are as good as examples, so don't waste your time if you know of a decent blog post somewhere. Thanks!

A: 

References: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard - Look for "12.5.7.1. Zend_Controller_Router_Route" for a clearer explanation.

This is not the best way to do it, but I have a working example. I welcome corrections. After seeing http://stackoverflow.com/questions/794126/shorten-zend-framework-route-definitions, I agree that named Routes should go in their own config (I use Django, and named Views/URLs are generally separated) - but here I'm just going to define Routes in my Bootstrap.

So, in Bootstrap.php, inside the Boostrap Class of course, I've created an function that will be automatically run, like so:

public function _initRoutes()
{
 $frontController = Zend_Controller_Front::getInstance();
 $route = new Zend_Controller_Router_Route(
  'login/', // The URL, after the baseUrl, with no params.
  array(
   'controller' => 'login', // The controller to point to.
   'action'     => 'index'  // The action to point to, in said Controller.
  )
 );
 $frontController->getRouter()->addRoute('loginpage', $route);
}

In the above example, "loginpage" will be the Name of the "Named Route".

So, inside my LoginController, (in a function that builds the form) instead of doing

$form->setAction('/blah/login')

I retrieve the URL of the named Route and pass that in, like so:

$form_action_url = $this->view->Url(array(), 'loginpage', true);
// -- SNIP --
$form->setAction($form_action_url) // ...

This may be pointless and wrong, but it seems to work at the moment. My reason for wanting a named URL, when Zend Framework handles URLs as /Controller/View(Action)/ automatically is because I'm anal about that kind of thing. I've been using Django for awhile, where the URLs are predefined, and I like it that way.

The Zend Framework MVC urls working out of the box is nice, tho.

Feel free to add notes and corrections to how this should work!

anonymous coward
There's no reason you can't setthe action in the View, which makes the syntax a little nicer.
Ciaran McNulty