热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Yii框架中给网页设置keywords和description的方法

Yii框架中给网页设置keywords和description的方法

If we want to set meta tags on a per page (controller-action) basis, we may use the?clientScript?application component.

Controller

Yii::app()->clientScript->registerMetaTag('This is an example', 'description');

    $this->render('example');

  }

}

?>

This will insert the description meta tag automatically into the layout when the render() function gets called.

Another solution would be to use a parent controller with the property $pageDescription. That way we don't have to make a function-call to set the description. Instead, we can easily modify the description inside of the controller. In the layout-file we'll simply echo the meta tag (if defined).

Parent-Controller

Controller

pageDescription = 'This is an example';

    $this->render('example');

  }

}

?>

Layout


pageDescription))
{
  echo 'pageDescription . '" />';
}

?>





As you can see, the second solution is probably better to work with because we can directly access $pageDescription and won't have to call an ugly long function every time. On the other hand, we have now additional php-code in the layout. Well that's not bad at all, but it could lead to problems.

What if we have 10 different layouts? What if we use several themes? In each of these files we would've to insert that snippet. Now let's think instead of a single string (the description), we would implement the same functionality for several rss-feed meta tags (an array). That would mean we have to do a foreach inside of the layout(s).

For a more flexible solution, we extend CController with a custom function.

beforeRender())
        {
            parent::render($view, $data, $return);
        }
    }

    public function beforeRender()
    {
        return true;
    }

}

?>

The parent-controller will make use of our extended CController and the special function.

pageDescription))
        {
            Yii::app()->clientScript->registerMetaTag($this->pageDescription, 'description');
        }

        return true;

    }

}

?>

The actual controller remains the same (like in the second solution).

pageDescription = 'This is an example';

    $this->render('example');

  }

}

?>

When the render() function gets called, ExtendedCController will first execute the custom beforeRender() function in our parent-controller to insert the meta tag and after that it will call the original CController::render() function.

That means with this solution we can access the $pageDescription property inside of the controller and we don't have to add any special code to the layout. Of course this solution comes in handy especially for things like the already noted rss-feeds.

Think about this:

$this->pageFeeds[] = 'http://example.com/feeds/recent.xml';
$this->pageFeeds[] = 'http://example.com/feeds/popular.xml';

I hope you enjoyed the read. Feel free to share your thoughts - thank you.

Tip:?Since the two classes?ParentController?and?ExtendedCController?are no real controllers (they only function as parent-classes), you have to put them into the components folder (egprotected/components).


推荐阅读
  • 本文汇集了多个专注于PHP及后端开发领域的技术博客,包括知名的技术社区如SegmentFault和掘金等。这些资源对于提升技术水平、拓宽知识视野具有重要价值。技术博客作为学习过程中的辅助材料,其作用在于激发兴趣、提供灵感,但更关键的是建立坚实的技术基础和丰富的实践经验。 ... [详细]
  • 随着科技的进步,AR智能眼镜正逐渐成为日常生活的一部分。今年冬天,一款仅重38克的AR智能眼镜成为了市场上的焦点,其超轻设计和创新功能值得我们深入了解。 ... [详细]
  • 本文探讨了在PHP中处理特定类型编码字符串的方法,特别是如何将HTML实体编码的字符串转换为普通文本。 ... [详细]
  • Java 动态代理详解与示例
    本文详细介绍了Java中的动态代理机制,包括如何定义接口、实现类和代理处理器,并通过具体示例演示了动态代理的创建和使用过程。 ... [详细]
  • LeetCode 6057: 计算与子树平均值相等的节点数量——深度优先搜索
    本题要求在给定的二叉树中找到所有符合条件的节点数量,即节点的值等于其所有后代节点(包括自身)值的平均值。这里的平均值是通过将所有后代节点值之和除以后代节点的数量,并向下取整得到。 ... [详细]
  • CSGO
    CSGOTimeLimit:40002000MS(JavaOthers)MemoryLimit:524288524288K(JavaOthers)ProblemDescriptio ... [详细]
  • 本文介绍了如何通过自定义View中的declare-styleable属性创建枚举类型,并在代码中访问这些枚举值的方法。 ... [详细]
  • 本文汇集了使用C#中不同HTTP客户端向Web API上传文件的实例,旨在为开发者提供实用的技术指南。 ... [详细]
  • 本文详细介绍了在PHP中如何创建新文件以及如何使自定义函数在整个项目中全局可用的方法,包括最新的实践技巧。 ... [详细]
  • 解决Android开发中的TextView难题
    探讨了在Android开发过程中遇到的关于TextView组件的常见问题,特别是如何实现多行文字的跑马灯效果,并提供了初步的解决方案和参考资料。 ... [详细]
  • Only2 Labs 是一家专注于视觉设计的工作室,如果您对当前的设计感到不满,或者急需寻找一个可靠的设计合作伙伴,甚至是您的团队项目需要专业指导,Only2 Labs 都将竭诚为您提供帮助。 ... [详细]
  • 解决phpMyAdmin运行错误:mysqli_init(): 属性访问尚未允许
    本文探讨了在使用phpMyAdmin过程中遇到的mysqli_init()函数错误,并提供了有效的解决方案。 ... [详细]
  • 本文探讨了Windows Presentation Foundation (WPF)如何通过扩展Microsoft Build Engine (MSBuild)来增强其构建能力,特别是在处理WPF特有的任务时。 ... [详细]
  • 本文探讨了在执行SQL查询时遇到的因字符集不同而导致查询结果差异的问题,特别是涉及中文字符时。文章分析了在不同字符集设置下,SQL查询结果的变化,并提供了详细的解决方案。 ... [详细]
  • 本文探讨了如何在JavaScript中调用PHP函数及实现两者之间的有效交互,包括通过AJAX请求、动态生成JavaScript代码等方法。 ... [详细]
author-avatar
风中凌乱2602938623
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有