Zend Framework 1 pass parameters using get to the route

989 views Asked by At

I hope the title does not sound too confusing, but I had no idea how to name my problem.

Brief intro:
I'm using Zend 1.1X.
At the moment I've been working with a search form sending few parameters via POST.

Now I have to change it to use GET, I have a route created looking similar to that:

"search/what/:what/shape/:shape" 

and so on, I also have 2 optional parameters which takes null as default.

I'm trying to generate an URL (using Zend View Helper Url) at form's action, but it throws an exception:

Uncaught exception 'Zend_Controller_Router_Exception' with message what is not specified

I Now don't have idea what should I do. If I change my route to "search" only, it then sends the form correctly, but I end up with "search?what=XXXX&shape=YYYY" instead of "search/what/XXXX/shape/YYYY".

Is there any way that could be handled the way I like??? :>

@EDIT

I think this should also be mentioned - I have a different form, similar one, pointing to a route without parameters specified as well and the uri gets "translated" to the form of "key/value" pairs. The only difference between them is that the first one does not use Url helper, instead has the method part hard-coded and my form is being submitted programatically (button => jQuery stuff => submit). Would that make a difference here, as I believe it should not? :>

I hope any possible source of this behaviour will come up to you, because I'm really stuck at the moment and I simply can't find what's wrong..

Thanks in advance!

1

There are 1 answers

2
doydoy44 On

With the GET method a form generates an url like this: action?param1=val1&param2=val2&.... I see two solutions:
The first is to regenerate the URL by javacsript, we can imagine something like this:

<form method="get" id="id_form">
....
</form>
<script> 
    var objet_form = document.getElementById('id_form');

    function gestionclic(event){
        var url = objet_form.action;
        for(var i = 0; i < objet_form.length; i++){
            url += "/" + objet_form[i].name + "/" + objet_form[i].value;
        }
        objet_form.action = url;
    }

    if (objet_form.addEventListener){
        objet_form.addEventListener("submit", gestionclic, false);
    } else{
        objet_form.attachEvent("onsubmit", gestionclic, false);
    }
</script>

But I don't think this is a good solution.

The second is to manage it with a plugin:

For the plugin, it must be declared in the bootstrap.
For example:

public function _initPlugins(){
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin(new Application_Plugin_PRoutage());
}

with this example, the application/plugins folder, create the PRoutage.php plugin like this:

class Application_Plugin_PRoutage extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {         
    ...
    }
}

and with the variable $request you have access to your data as an array with $request->getParams().
We can imagine something like this:

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{         
    $param = $request->getParams();
    $what = "";
    $shape = "";
    if (isset($param['what'])  $what  = $param['what'];
    if (isset($param['shape']) $shape = $param['shape'];

    if ($what == "XXXX" && $shape == "YYYY"){
        $request->setControllerName('other_controler')
                ->setActionName('other_action')
                ->setDispatched(true) ;
    }
}

I hope it will help you