简体   繁体   English

锂PHP全局变量

[英]Lithium PHP global variables

I have looked all over the place, but still cant figure out how to set a global variable in PHP (Lithium framework). 我已经遍布各处,但仍无法弄清楚如何在PHP(Lithium框架)中设置全局变量。

My goal is to make the server root always accessible everywhere, without having to write this code everytime, to make my app independent of the hostname it is running on : 我的目标是使服务器root始终可以在任何地方访问,而不必每次都编写此代码,以使我的应用程序独立于其运行的主机名:

<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

Please don't tell me to just use absolute or relative paths, as I have tried everything that way, and it is simply too messy (besides not working most of the time). 请不要告诉我只使用绝对路径或相对路径,因为我已经尝试了所有方式,而且它太乱了(除了大部分时间都不工作)。

It looks like your code above is in a template. 看起来上面的代码在模板中。 So you can use $this->request()->to('url') to get the current url. 所以你可以使用$this->request()->to('url')来获取当前的url。 Or from a controller, it would be $this->request->to('url') . 或者从控制器,它是$this->request->to('url') You can also pass 'absolute' => true to the options for Router::match . 您还可以将'absolute' => true传递给Router::match的选项。 Unfortunately, the html link helper - ie $this->html->link(...) - doesn't pass the 'absolute' => true option through to the Router::match but you can call $this->url() which is passed through to Router::match . 不幸的是,html链接助手 - 即$this->html->link(...) - 没有将'absolute' => true选项传递给Router::match但你可以调用$this->url()传递给Router::match $this->url() So it would be $this->url('/relative/url', array('absolute' => true)) . 所以它是$this->url('/relative/url', array('absolute' => true))

See the docs for Router::match 请参阅Router :: match文档

The Request object also provides access to things like http host and request uri. Request对象还提供对http host和request uri之类的访问。 See the docs for Request::env() 请参阅Request::env()文档

But, to your original point about global vars, I think the Environment class should be used for this. 但是,关于全局变量的原始观点,我认为应该使用Environment类。 You can put arbitrary data in it -- and even set it in your bootstrap to have different values for different environments (ie dev vs. staging vs. production). 您可以在其中放置任意数据 - 甚至可以在引导程序中将其设置为具有不同环境的不同值(即开发与分段与生产相比)。

Sorry, but use relative paths and use the link generator helper, which won't get what you're trying to do wrong. 抱歉,但使用相对路径并使用链接生成器帮助程序,这将无法获得您尝试做错的内容。

See the docs . 查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM