如何在bootstrap中访问URL参数

发布于 2024-08-08 12:07:07 字数 267 浏览 13 评论 0原文

我试图在引导文件中捕获 URL 参数,但经过多次尝试后我无法做到这一点。

我已经尝试过,但它不起作用:

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

这是正确的方法吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2024-08-15 12:07:07

您将无法从引导程序访问请求参数,因为它尚未完成调度/路由过程。我认为使用 控制器插件,根据 URL 执行操作是他们最擅长的。或者,如果您绝对必须在引导程序中执行此操作,则可以使用 getRequestUri()$_GET,或者您可以编写一个快速脚本来自己解析 url。

编辑:

在我弄清楚插件如何工作之前,我过去做过一些类似这样的愚蠢的事情:

/**
 * 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;
}

这至少会给你一个可以切换的模块名称在引导程序中。不过,通过正确完成插件,您应该能够做任何您需要的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文