Ex:
echo getMyUrl();
should echo:
http://localhost/myapplication
Ex:
echo getMyUrl();
should echo:
http://localhost/myapplication
$myURL = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
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.
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.