-- php <[EMAIL PROTECTED]> wrote
(on Saturday, 03 March 2007, 05:19 PM -0600):
> I'm sorry, Im trying to get this working But I can't get my route to 
> route.  But I have a few questions:
> 
> What exactly is the Assemble Method supposed to return? Looking at Route 
> I see this comment:
> Assembles user submitted parameters forming a URL path defined by this 
> route
> 
> What Submitted Parameters? who is 'user' in this case?

It would be used for instance in the Zend_View Url helper to build URLs
in your view scripts:

    // within a view script
    <a href="<?= $this->url(array('cat' => 'foo'), 'cat') ?>">Foo
    category</a>

> Also I have two routes right now: Here is what they look like.
> 
> require_once('Chad/CatRoute.php');
> $rwRouter->addRoute(':cat/*', new Chad_CatRoute());

Looks like you're not quite understanding how routes are added to the
router. The first argument is simply a route name -- just a descriptor.

I'd write the above line like this:

    $rwRouter->addRoute('cat', new Chad_CatRoute());

Note the absence of any path information in the first argument -- it's
just supposed to describe the route so you may reference it later. It's
a name.

> $rwRouter->addRoute('test', new 
> Zend_Controller_Router_Route('test/:action', array('controller' => 
> 'test', 'action' => 'index')));
> 
> Where Chad_CatRoute is my new Route Defined with 
> Zend_Controller_Route_Interface.
> 
> How can i get that route to route to CatController IndexAction?

By hardcoding that information in your route object as the defaults it
will return for the controller and action keys.

In the example I gave earlier, I neglected to show how defaults might be
used. Here's a stab at it:

    class CategoryRoute implementes Zend_Controller_Router_Route_Interface
    {
        public $defaults = array(
            'controller' => 'cat',
            'action'     => 'action'
        );

        public function __construct(array $defaults = array())
        {
            if (!empty($defaults)) {
                $this->defaults = array_merge($this->defaults, $defaults);
            }
        }

        public function match($path)
        {
            if (preg_match_all('#^/([^/]*/?\)+$#', $path, $matches)) {
                $segments = $matches[1];
                $category = array_shift($segments);
                $return = array(
                    'category'      => $category,
                    'subCategories' => $segments
                );

                // This part merges in the defaults
                $return = array_merge($this->defaults, $return);
                return $return;
            }

            return false;
        }

        // implement assemble at some point
    }

Basically, the array returned by match() are the key/value pairs that
will be set in the request object by the router. Since your route
doesn't actually set the controller/action, those will be defined by
defaults in the object; if you pass in defaults, it will merge those in
with the defaults in the class, overwriting them if necessary.

Hope that helps!

> Matthew Weier O'Phinney wrote:
> >-- php <[EMAIL PROTECTED]> wrote
> >(on Thursday, 01 March 2007, 10:49 PM -0600):
> >  
> > > I am trying to make a app that has an arbitrary number of categories, I 
> > > want the categories to be represented in the url like:
> > > http://www.example.org/category/cat1/cat2/cat3/cat4
> > > in heirarchal order.
> > > 
> > > So I would like to make a router that would point to the category 
> > > Controller, index action by default. _getParam() would return the array 
> > > of the rest of the url.
> > > 
> > > Is there a way I can do this? Any Ideas where I could get started?
> >
> > A couple possibilities. First, you could create a route to attach to the
> > router:
> > 
> >     $router = $front->getRouter();
> >     $router->addRoute(
> >         'category', 
> >         new Zend_Controller_Router_Route(
> >             ':category/*',
> >             array('controller' => 'category', 'action' => 'index')
> >         )
> >     );
> > 
> > However, this wouldn't give you back the array of URL parameters.
> > 
> > So, next step is to create your own route -- not router. Try something
> > like this (untested):
> > 
> >     class CategoryRoute implementes Zend_Controller_Router_Route_Interface
> >     {
> >         public function match($path)
> >         {
> >             if (preg_match_all('#^/([^/]*/?\)+$#', $path, $matches)) {
> >                 $segments = $matches[1];
> >                 $category = array_shift($segments);
> >                 $return = array(
> >                     'category'      => $category,
> >                     'subCategories' => $segments
> >                 );
> >                 return $return;
> >             }
> > 
> >             return false;
> >         }
> > 
> >         // you also need an assemble method, but I'll leave that to you
> >     }
> > 
> > Then, in your action controller, you'd use:
> > 
> >     $category = $this->_getParam('category', $default);
> >     $subCats  = $this->_getParam('subCategories', array());
> > 
> > HTH!

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to