【问题标题】:CakePHP - Anchor class not changing (undefined variable $action)CakePHP - 锚类不变(未定义的变量 $action)
【发布时间】:2021-12-18 00:13:22
【问题描述】:

我想在我的锚链接处于活动状态但无法正常工作时为其添加一个类。那么我该如何声明 $action 变量呢?

版本:

 4.2.9

错误:

Undefined variable: action [ROOT\templates\layout\default.php, line 108]

模板/布局/default.php

<li>
    <?= $this->Html->link(
        '<div class="icon"></div>'.__('Summary'),
        ['controller' => 'modules', 'action' => 'summary'],
        [
        'escapeTitle' => false,
        'class' => 'icon-summary '.( ($controller == 'Modules' && $action == 'summary') ? 'active' : '')
        ]
    ) ?>
</li>

在我的 ModulesController.php 中

  public function summary()
  {
      $modules = $this->getAllModules();
  }

来自 DebugKit 的路由参数

'controller' => 'Modules',
'action' => 'summary',
'pass' => [ ],
'plugin' => null,
'_matchedRoute' => '/{controller}/{action}/*',
'_ext' => null,
]

【问题讨论】:

  • 这是因为这里没有定义名为$action 的变量。路由中有一个名为 action 的组件,但您还没有从那里提取它。
  • 我必须在哪里提取它?在我的 ModulesController 中?

标签: php html model-view-controller cakephp


【解决方案1】:

在我的AppController.php 中,我在beforeRender() 函数中插入了下面的代码。现在我可以访问action 变量了。

$this->set('action', $this->request->getParam('action'));

非常感谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 2018-03-04
    相关资源
    最近更新 更多