tags:

views:

73

answers:

4

Ex:

echo getMyUrl();

should echo:

http://localhost/myapplication
A: 

$myURL = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];

Martin Hohenberg
CakePHP should have a config variable for that btw, you should check it out, but the above answer works as well.
yoda
that'll output the directory upto "../webroot/index.php"my current workaround is to explode the webroot part
zero juan
there is no config variable that I know of that points to the current page with the protocol and hostname etc.Also, try and avoid hitting the _$SERVER global directly. Use env( 'key' ) instead. This is a cake internal function that works around $_SERVER and other superglobal availability issues and returns a consistent result across many more scenarios than relying on $_SERVER does.
Abba Bryant
+3  A: 

You could use Router::url to get the current url.

If you leave both params blank, you will get the relative path to the current controller and action.

Router::url(); will return /myapplication/users/register

Setting the second param to true will return the full url.

Router::url(null, true); will return http://localhost/myapplication/users/register

You can also use the first param to set the controller and action you want the url to contain. Pass either a string or an array similar to the HTML helper's url method.

Take a look router class in the API for more info.

DanCake
A: 

From the view you can use:

echo $html->url('/');
jimiyash
A: 

From inside a view file you can use

<?php
    // view /app/views/pages/home.ctp
    // assuming default page routes and an app located in WEB_FOLDER/myApplication
    debug( join( '', array( 'http://', env( 'SERVER_NAME' ), $this->here )));
    debug( join( '', array( 'http://', env( 'SERVER_NAME' ), $html->url( $this->here ))));
    debug( join( '', array( 'http://', env( 'SERVER_NAME' ), $html->url( ))));
    debug( join( '', array( 'http://', env( 'SERVER_NAME' ), $html->url( '/' ))));
    debug( join( '', array( 'http://', env( 'SERVER_NAME' ), $html->url( array( 'controller' => 'pages', 'action' => 'display', 'home' )))));
?>

should return (when viewing the homepage from http://servername/)

http://servername/

except in the last case (since your aren't using $html->link the routes don't seem to be translated) where the route for http://pages/display/home isn't being translated in reverse to http://servername/ - in this case the returned string will be

http://servername/pages/display/home

If you view the homepage from servername/pages/display/home you should also note that the $html->url( '/' ) call won't translate the '/' into a controller action pair. You will literally get the '/' appended to the servername.

Abba Bryant