CakePHP:如何在视图中使用Controller :: referer()

时间:2013-04-17 20:48:45

标签: php cakephp cakephp-2.0

我收到以下错误:

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]

由此引起:

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

为什么?

2 个答案:

答案 0 :(得分:14)

你没有。您改为使用请求对象:

$this->request->referer();

控制器在内部不执行任何其他操作。

小心:引用者可能是空的,因此在这种情况下你可能想要提供一个后备。

还要注意可选的param $ local:

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

答案 1 :(得分: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 
}

如果使用Router::parse($referer_url)时遇到任何错误,请在控制器中添加cakephp路由

use Cake\Routing\Router;