开发者

How do I access programmatically the url where my index is located in CakePHP? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question mo开发者_如何学Gore broadly applicable, visit the help center. Closed 9 years ago.

Ex:

echo getMyUrl();

should echo:

http://localhost/myapplication


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.


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


From the view you can use:

echo $html->url('/');


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜