Actually,

it turns out, it was a lot of over thinking.  You don't need to extend or
create a new view helper URL at all.

This is all you have to do:

1. In your action script, pass the parameters to your view script so you can
use them (maybe there's a way of accessing them in your view script too, but
I don't recall at the moment).

$this->view->params = $this->_request->getParams();

2. In your view script, you can ksort your params, and pass them to the
normal URL view helper, *with reset set to true*.

ksort($this->params);
echo '< a href="' . $this->url($url_params, null, true) . '">My Link< /a >';

That should produce a sorted URL.

The goal here is to be able to auto-generate URL links for attributes and
have those links dynamically update based on the current page URL and the
future page URL.


----


Now, that seems all well and good, but here's how I'm using it in my
application.

Objective:
Create a product selector that allows a visitor to select product
attributes, such as Size, Color, Price, etc.  Each attribute category can
have multiple values.  But only one will be 'active' at a given time.

Design Choices:
The product attribute list will be links, not an HTML form.  This is similar
to how Amazon, Home Depot and Lowes websites work.

Function:
Clicking a link will load the page it's linked to.  That page is simply the
product selector Controller/Action with parameters.  The parameters are
based on the link clicked.  "Active" parameters will have "remove" links, to
remove these parameters from the URL.

A new list of products will be displayed based on the product attribute
added or removed.

Functional Example:
User clicks "size = large" and the page reloads to display all products of
size large.
User clicks "color = red" and the page reloads to display all large products
of size red.

Technical Example:
user click "size = large".  Page reloads to "/product/selector/size/large/"
user clicks "color = red".  Page reloads to
"/product/selector/color/red/size/large" (note, this URL is sorted)

Code: (note, there's probably more than one way to skin this cat)
1. In the action controller, pass the parameters to the view script:
$this->view->params = $this->_request->getParams();

2. In the view script, loop through your list of product attributes.  This
assumes you have an associative array like this:

product_attributes
(
    [color] => Array
        (
            [0] => red
            [1] => blue
            [2] => green
        )
    [size] => Array
        (
            [0] => small
            [1] => medium
            [2] => large
        )
    [...]

foreach($product_attribute as $category=>$values){
    foreach($values as $attribute){
        [...]
    }
}

3. For each attribute
   3a. append the name:value pair to the parameter list
   3b. ksort the list
   3c. use the URL view helper to create the url

$this->params[$category] = $attribute;
ksort($this->params);
echo '< a href="' . $this->url($this->params, null, true) . '">$attribute<
/a >';

That should produce a sorted URL.

Ok, for the deeper programmers out there, here's why I think this works this
way, from what I've observed.

The URL view helper calls the Router object, which there are several
different flavors such as Rewrite and Route.  In my tests, it showed that
the Router Rewrite object was being loaded and then loading the a Route
object, for which there are several, Module, Static, Chain, Hostname, etc.

In this case, I found that Router->Route->Module was being used for the
"assemble()" function.  So, I looked through that code a bit.

Long story short, the Route object does this (again, this is as a understand
it):

1. Get the current parameters. (an unsorted list)
2. Replace the values of the current parameters with the passed in
parameters.
* note * because we're only replacing values, we end up with the same
unsorted list that was pulled in step 1.
2b. As part of replacing the values, it appends any new parameters that were
passed in.  In other words, it loops over the passed in parameters and
simply updates the values, or adds new, parameters.

In the end, even if you pass in a sorted list, you end up with an unsorted
list because the object pulls an unsorted list to update.

Key
If you set the "reset" flag, the Route object won't pull a parameter list
(as seen in step 1 previously).  Instead, it just generates a list based the
passed in parameters.

As part of doing so, it first checks to see if the Module, Controller, and
Action parameters were passed in and sets them appropriately if they were
not found and unsets them from the array if they were found (after adding
them to the generated URL string).

Then, it starts looping over the extra query parameters.  Since these were
passed in in a sorted order, they will be added in that order.

Of course, this might work differently if you use a different Route object. 
The one I'm using is the Module Route object.  I'm not sure if weird things
will happen if you start applying translations and what not.

But this is what I've researched and understood so far.  I might not be 100%
accurate on my understanding, but it seems to be working as I would expect.

If anyone else has more to add to this, or correct any mistakes, please do.

Cheers!
Fozzy
-- 
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/Generating-URL-s-View-Helper-Action-Helper-tp3002936p3004662.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to