cakephp 从助手中访问视图属性/变量

发布于 2024-09-15 12:38:34 字数 234 浏览 5 评论 0原文

访问视图属性“passedArgs”(或任何类似的属性)

/* view */
$this->passedArgs

有没有一种合理的方法可以从助手中

?我很乐意自定义帮助程序的 _construct() 或自定义 app_helper...但我不想在每次调用时都将 $this->passedArgs 传递到帮助程序中查看或使用。

is there a reasonable way to access the view attribute "passedArgs" (or any similar)

/* view */
$this->passedArgs

from within a Helper?

I'd be happy to customize the _construct() of the helper or to customize the app_helper... but I don't want to have to pass $this->passedArgs into the helper on every view or usage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧桑㈠ 2024-09-22 12:38:34

Cake 2.x 和 3.x

您可以在 _View 对象中查找变量:

$this->_View->viewVars['foo'];

Cake 1.x

如果您抓取当前视图从帮助程序中获取对象,您应该能够获取其 passArgs。

class SomeHelper extends AppHelper {
  function __construct($settings = array()){
    $this->passedArgs = ClassRegistry::getObject('view')->passedArgs;
  }
}

Cake 1.2.x

如果您从助手中获取当前视图对象,您应该能够获取其 viewVars。

class SomeHelper extends AppHelper {
  function __construct($settings = array()){
    $this->viewVars = ClassRegistry::getObject('view')->viewVars;
  }
}

享受,
缺口

Cake 2.x and 3.x

You can look up your variables in the _View object:

$this->_View->viewVars['foo'];

Cake 1.x

If you grab the current view object from within the helper you should be able to get to its passedArgs.

class SomeHelper extends AppHelper {
  function __construct($settings = array()){
    $this->passedArgs = ClassRegistry::getObject('view')->passedArgs;
  }
}

Cake 1.2.x

If you grab the current view object from within the helper you should be able to get to its viewVars.

class SomeHelper extends AppHelper {
  function __construct($settings = array()){
    $this->viewVars = ClassRegistry::getObject('view')->viewVars;
  }
}

Enjoy,
Nick

绝情姑娘 2024-09-22 12:38:34

蛋糕3:

$this->getView()->get('my_var');

Cake 3:

$this->getView()->get('my_var');
神也荒唐 2024-09-22 12:38:34

您是否尝试过仅从 AppController 设置视图的值?

class AppController extends Controller {
 function beforeFilter() {
  // other stuff
  $this->set( 'passed_args', $this->params['pass'] );
 }
}

Have you tried just setting the view's value from the AppController?

class AppController extends Controller {
 function beforeFilter() {
  // other stuff
  $this->set( 'passed_args', $this->params['pass'] );
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文