How to access URL parameters in bootstrap

elbicho picture elbicho · Oct 20, 2009 · Viewed 7.6k times · Source

I'm trying to capture a URL parameter in my bootstrap file but after several attempts I'm not able to do it.

I've tried this but it does not work:

protected function _initGetLang() {
  $frontController = Zend_Controller_Front::getInstance();
  $lang= $frontController->getParam('lang');
}

Is this the right way to do it?

Thks.

Answer

typeoneerror picture typeoneerror · Oct 20, 2009

You won't be able to access the request params from the bootstrap because it hasn't yet gone through the dispatch/routing process. I think you'd be better served by using a Controller Plugin, performing actions based on the URL is what they do best. Or if you absolutely have to do it in the bootstrap, getRequestUri() or $_GET is available, or you could write a quick script to parse the url yourself.

Edit:

I've done some silly stuff like this in the past before I figured out how plugins work:

/**
 * Grab the module name without a request instance
 *
 * @return string  The module name
 */
public static function getModuleName()
{
    $uri = ltrim($_SERVER["REQUEST_URI"], "/");
    $module = substr($uri, 0, strpos($uri, "/"));
    return $module;
}

This would at least give you a module name that you could switch on in the bootstrap. You should be able to do anything you need with the plugins done correctly though.