How to get complete current url for Cakephp

前端 未结 24 1219
长情又很酷
长情又很酷 2020-12-08 00:10

How do you echo out current URL in Cake\'s view?

相关标签:
24条回答
  • 2020-12-08 00:40

    The Cake way for 1.3 is to use Router::reverse:

    Link to documentation

    $url = Router::reverse($this->params)
    echo $url;
    

    yields

    /Full/Path/From/Root/MyController/MyAction/passed1/named_param:bob/?param1=true&param2=27
    
    0 讨论(0)
  • 2020-12-08 00:40

    All previously proposed approaches didn't satisfy my requirements for getting a complete URL (complete as in qualified) e.g. to be used in an email send from controller action. I need the scheme and hostname as well then, and thus stumbled over the following approach:

    <?php echo Router::url( array( $id ), true ) ?>
    

    Due to providing router array current controller and action is kept, however id isn't and thus has to be provided here again. Second argument true is actually requesting to prepend hostname etc. for getting full URL.

    Using Router::url() is available in every situation and thus can be used in view files as well.

    0 讨论(0)
  • 2020-12-08 00:41

    In the request object you have everything you need. To understand it:

    debug($this->request->url);
    

    and in your case

    $here = $this->request->url;
    
    0 讨论(0)
  • 2020-12-08 00:43

    In View:

    Blank URL: <?php echo $this->Html->Url('/') ?>
    Blank Full Url: <?php echo $this->Html->Url('/', true) ?>
    Current URL: <?php echo $this->Html->Url($this->here) ?>
    Current Full URL: <?php echo $this->Html->Url($this->here, true) ?>
    

    In Controller

    Blank URL: <?php echo Router::url('/') ?>
    Blank Full Url: <?php echo Router::url('/', true) ?>
    Current URL: <?php echo Router::url($this->request->here()) ?>
    Current Full URL: <?php echo Router::url($this->request->here(), true) ?>
    
    0 讨论(0)
  • 2020-12-08 00:44

    You can do either

    From a view file:

    <?php echo $this->request->here() ?>">
    

    Which will give you the absolute url from the hostname i.e. /controller/action/params

    Or

    <?php echo Router::url(null, true) ?> 
    

    which should give you the full url with the hostname.

    0 讨论(0)
  • <?php echo $_SERVER[ 'REQUEST_URI' ]; ?>
    

    EDIT: or,

    <?php echo $this->Html->url( null, true ); ?>
    
    0 讨论(0)
提交回复
热议问题