Question

I have looked all over the place, but still cant figure out how to set a global variable in PHP (Lithium framework).

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 :

<?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).

Was it helpful?

Solution

It looks like your code above is in a template. So you can use $this->request()->to('url') to get the current url. Or from a controller, it would be $this->request->to('url'). You can also pass 'absolute' => true to the options for Router::match. Unfortunately, the html link helper - i.e. $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. So it would be $this->url('/relative/url', array('absolute' => true)).

See the docs for Router::match

The Request object also provides access to things like http host and request uri. See the docs for Request::env()

But, to your original point about global vars, I think the Environment class should be used for this. You can put arbitrary data in it -- and even set it in your bootstrap to have different values for different environments (i.e. dev vs. staging vs. production).

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top