Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework - Optional Router Labels

Very likely I'm going about this in the wrong way entirely. I'm completely new to the framework..

The site I am developing has two "parts" that are mainly separate. An informational/community half, and a commerce half. I'm using the following directory structure:

--application
----default
------controllers
------layouts
------models
------views
----store
------controllers
------layouts
------models
------views
--config
--library
--public

I would like to have a URL structure when browsing for products as follows:

/view/category/model/revision

This would pull up a specific product/revision - but I would like to back-track as well (browsing all revisions, all models, etc). I can't figure out how to achieve this.. My route is setup like this:

Bootstrap.php

  $front = Zend_Controller_Front::getInstance();
  $router = $front->getRouter();
  $route = new Zend_Controller_Router_Route(
      'view/:cid/:sku/:rev',
      array('module' => 'store', 'controller' => 'index', 'action' => 'index')
  );
  $router->addRoute('view', $route);

This works fine for pulling up a specific product, but throws an exception (it reverts to the default module and complains that the controller 'view' does not exist) when leaving out any of the 3 labeled parameters. Is it possible to put in optional labels, where it would continue to use the view controller under the store module for 1-3 parameters? Am I missing the point?

I found nothing in the framework docs, but I wouldn't be surprised if I just couldn't find the page.. There's something about the Zend Framework documentation that drives me crazy.

Thank You

like image 687
Mahdi.Montgomery Avatar asked Sep 10 '26 12:09

Mahdi.Montgomery


1 Answers

I'm not really a ZendFramework guy, but it's obvious the missing parameters are causing the issue. Routes are matched in reverse order. Could it be passing a NULL value to the view when 3 parameters are passed and it is expecting 4?

What if you tried something like:

$route = new Zend_Controller_Router_Route(
      'view/:cid/:sku/:rev',
      array('module' => 'store', 'controller' => 'index', 'action' => 'index', 'cid' => 0, 'sku' => 0, 'rev' => 0)
  );

It should pass default values if they are not provided.

like image 95
Chuck Burgess Avatar answered Sep 13 '26 03:09

Chuck Burgess



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!