3

As you know, Zend Framework (v1.10) uses routing based on slash separated params, ex.

[server]/controllerName/actionName/param1/value1/param2/value2/

Queston is: How to force Zend Framework, to retrive action and controller name using standard PHP query string, in this case:

[server]?controller=controllerName&action=actionName&param1=value1&param2=value2

I've tried:

protected function _initRequest()
{
    // Ensure the front controller is initialized
    $this->bootstrap('FrontController');

    // Retrieve the front controller from the bootstrap registry
    $front = $this->getResource('FrontController');

    $request = new Zend_Controller_Request_Http();
    $request->setControllerName($_GET['controller']);
    $request->setActionName($_GET['action']);
    $front->setRequest($request);

    // Ensure the request is stored in the bootstrap registry
    return $request;
}

But it doesn't worked for me.

2 Answers 2

3
$front->setRequest($request);

The line only sets the Request object instance. The frontController still runs the request through a router where it gets assigned what controller / action to call.

You need to create your own router:

class My_Router implements Zend_Controller_Router_Interface
{
    public function route(Zend_Controller_Request_Abstract $request)
    {
        $controller = 'index';
        if(isset($_GET['controller'])) {
            $controller = $_GET['controller'];
        }

        $request->setControllerName($controller);

        $action = 'index';
        if(isset($_GET['action'])) {
            $action = $_GET['action'];
        }

        $request->setActionName($action);
    }
}}

Then in your bootstrap:

protected function _initRouter()
{
    $this->bootstrap('frontController');
    $frontController = $this->getResource('frontController');

    $frontController->setRouter(new My_Router());
}
Sign up to request clarification or add additional context in comments.

2 Comments

It almost worked, I had to extend My_Router from Zend_Controller_Router_Rewrite or implement remaining interface methods. First solution was quicker;) Now it works great. Thx!
According to the manual you were supposed to only have to implement that method. The API seemed to say otherwise so I was confused. You might also try extending Zend_Controller_Router_Abstract instead of Zend_Controller_Router_Rewrite. They need a better example in the manual.
1

Have you tried: $router->removeDefaultRoutes(), then $request->getParams() or $request->getServer()?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.