gpt4 book ai didi

php - Phalcon PHP - 在 View 中获取 Controller 名称

转载 作者:行者123 更新时间:2023-12-03 13:52:07 24 4
gpt4 key购买 nike

我有一个带有顶部导航栏的站点布局,我需要根据在用户单击不同页面时更改的 Controller 将类设置为事件状态。

View ->index.phtml

 <li class='<?php echo (isset($this->controllerName) && $this->controllerName == 'about') ? 'active' : '' ?>'><a href="/about">About Us</a></li>
<li class='<?php echo (isset($this->controllerName) && $this->controllerName == 'services') ? 'active' : '' ?>'><a href="/services">Member Sevices</a></li>

我希望 Phalcon 有一个 View 函数,或者我可以在 bootstrap 中放置一些东西,这样它就适用于所有页面,而我不必记住在每个 Controller 中手动设置 controllerName 变量。

最佳答案

在您的 View 中访问您的路由器服务:

$this->router->getControllerName()
$this->router->getActionName()

或者如果您使用的是伏特,则可以使用简写
router.getControllerName()
router.getActionName()

这将适用于您的用例,但如果您发现菜单逻辑变得过于笨拙,则有多种方法可以实现您的目标。

我发现最简单的方法是使用 javascript,但如果他们禁用了 js(1%),则没有后备。如果您想覆盖 100% 的受众,您可以使用 Phalcon 团队在 INVO 中使用元素库所做的方法: https://github.com/phalcon/invo/blob/master/app/library/Elements.php

关于php - Phalcon PHP - 在 View 中获取 Controller 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23665433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com