CakePHP: how to use Controller::referer() in a view
Asked Answered
A

2

5

I'm getting the following error:

Strict (2048): Non-static method Controller::referer() should not be called statically,
assuming $this from incompatible context [APP/View/Questions/admin_edit.ctp, line 20]

Caused by this:

//in app/View/Questions/admin_edit.ctp
echo $this->Html->link('Cancel', Controller::referer() );

Why?

Aphelion answered 17/4, 2013 at 20:48 Comment(0)
M
15

You don't. You use the request object instead:

$this->request->referer();

The controller does nothing else internally.

Careful: the referer can be empty and thus you might want to provide a fallback here in that case.

Also note the optional param $local:

@param boolean $local If true, restrict referring URLs to local server

Mauldon answered 17/4, 2013 at 20:50 Comment(2)
Thanks. For some reason when I enter the URL directly (no referer) it returns my document root (example.com) instead of my fallback. Any idea why?Aphelion
This happens to me too. On my local copy, $this->referer() works fine, but on the server, it's just spitting out the domain name.Phip
B
0
$referer_url = $this->referer('/', true); // you will get like (without base URL) "/users/profile"
$refererController = Router::parse($referer_url); // this will return $referer_url as array which contains 

Array ( 
   [controller] => users 
   [action] => profile 
}

if anyone face any error when you use Router::parse($referer_url) please add cakephp routing in your controller

use Cake\Routing\Router;
Basenji answered 25/9, 2020 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.