CakePHP - How to get public path to application root
I'm looking for a cons开发者_如何学Ctant or variable that will provide a public path to my application root.
I have got so far as FULL_BASE_URL
which gives me http://www.example.com
but I have the added problem of my application being in a sub directory (e.g. http://www.example.com/myapp/
).
Is there any way to get the path like http://www.example.com/myapp/
in my controller?
$this->Html->url( '/', true );
In general you should generate all links with that function, see http://book.cakephp.org/view/1448/url
$this->base;
http://api.cakephp.org/class/dispatcher
<?php
...
$this->redirect( Router::url( "/", true ));
...
?>
Router is the static class used by the HtmlHelper::link, Controller::redirect etc. the Router::url method takes a string, or array and matches it to a route. Then it returns the url that matched the route info as a string.
If you pass "/" to the Router::url call you get a relative link to the root of your app. If you pass "/" and true to the Router::url call you will prepend the full BASE_URL to the resulting relative path. This should give you what you need. If not, here is the link to the Router documentation. Try experimenting with the second boolean param - it may or may not work as expected depending on what you read / your own testing.
http://api.cakephp.org/class/router#method-Routerurl
精彩评论