1条回答 默认 最新
doutingyou2198 2017-06-02 04:16关注At our web development company HeavyDots we've got into this requirement too, specially because we wanted to put SEO related tags first so that Google can find them right at the beginning.
Basically this is what you need to:
1) Extend the core ClientScript component at framework\web\CClientScript.php
Create the file
protected\components\CustomClientScript.phpand put in the following contents:<?php /** * This class extends framework\web\CClientScript.php * and overrides some of its methods. */ class CustomClientScript extends CClientScript { /** * Inserts the scripts in the head section. * @param string $output the output to be inserted with scripts. */ public function renderHead(&$output) { $html = ''; foreach ($this->metaTags as $meta) $html .= CHtml::metaTag($meta['content'], null, null, $meta) . " "; foreach ($this->linkTags as $link) $html .= CHtml::linkTag(null, null, null, null, $link) . " "; foreach ($this->cssFiles as $url => $media) $html .= CHtml::cssFile($url, $media) . " "; foreach ($this->css as $css) $html .= CHtml::css($css[0], $css[1]) . " "; if ($this->enableJavaScript) { if (isset($this->scriptFiles[self::POS_HEAD])) { foreach ($this->scriptFiles[self::POS_HEAD] as $scriptFile) $html .= CHtml::scriptFile($scriptFile) . " "; } if (isset($this->scripts[self::POS_HEAD])) $html .= CHtml::script(implode(" ", $this->scripts[self::POS_HEAD])) . " "; } if ($html !== '') { $count = 0; // $output = preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is', '<###head###>$1', $output, 1, $count); $output = preg_replace('/(<\\/title\s*>)/is', "$1 <###head###>", $output, 1, $count); if ($count) $output = str_replace('<###head###>', $html, $output); else $output = $html . $output; } } }2) Change your configuration so that the application will start using it:
Add the new component in your configuration file
protected\config\main.php:... // application components 'components' => array( ... 'clientScript' => array( 'class' => 'application.components.CustomClientScript', ), ...Additional notes
Extending application components
What we have just done is a very common way of customizing Yii without editing core files and as you see it's easy to implement.
For every application component that you wish to extend just look for the original one in the core files and then make your own.
We've placed this one in
protected\componentsbut you could have placed it wherever you want if your application is big and you want to have a better organized structure.As you can see in this component we've overriden the
renderHeadmethod and changed just one line that dictates where to insert the head-related code.We've made it save it just after
</title>to obtain the exact requirement that you have asked for, but as you can guess you could play and change things if you want a different behaviour.What if you already had a custom client script
In many of our projects we were already using a custom client script, for example, the minScript extension.
In this case, in order to keep both we did this:
1) Leave the minScript component in the configuration instead of our custom one
'clientScript' => array( 'class' => 'ext.minScript.components.ExtMinScript', ), // 'clientScript' => array( // 'class' => 'application.components.CustomClientScript', // ),2) Make minScript component extend from ours
//class ExtMinScript extends CClientScript { class ExtMinScript extends CustomClientScript {Or if you don't want to change minScript code:
1) Autoload the minScript component class by adding this into the
importsection of the configuration:'ext.minScript.components.*',2) Extend your component from minScript component instead of core one
Important: Since minScript also has its own implementation of
renderHeadmethod in order to keep both you will have to bring the minScript block of code to your component.<?php /** * This class extends framework\web\CClientScript.php * and overrides some of its methods. */ class CustomClientScript extends ExtMinScript { /** * Inserts the scripts in the head section. * @param string $output the output to be inserted with scripts. */ public function renderHead(&$output) { // minScript block $this -> _minScriptProcessor('scripts', self::POS_HEAD); $this -> _minScriptProcessor('css'); // end minScript block $html = ''; foreach ($this->metaTags as $meta) $html .= CHtml::metaTag($meta['content'], null, null, $meta) . " "; foreach ($this->linkTags as $link) $html .= CHtml::linkTag(null, null, null, null, $link) . " "; foreach ($this->cssFiles as $url => $media) $html .= CHtml::cssFile($url, $media) . " "; foreach ($this->css as $css) $html .= CHtml::css($css[0], $css[1]) . " "; if ($this->enableJavaScript) { if (isset($this->scriptFiles[self::POS_HEAD])) { foreach ($this->scriptFiles[self::POS_HEAD] as $scriptFile) $html .= CHtml::scriptFile($scriptFile) . " "; } if (isset($this->scripts[self::POS_HEAD])) $html .= CHtml::script(implode(" ", $this->scripts[self::POS_HEAD])) . " "; } if ($html !== '') { $count = 0; // $output = preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is', '<###head###>$1', $output, 1, $count); $output = preg_replace('/(<\\/title\s*>)/is', "$1 <###head###>", $output, 1, $count); if ($count) $output = str_replace('<###head###>', $html, $output); else $output = $html . $output; } } }Final note
Remember to re-check your implementation if you update Yii or minScript in the future so there are no changes that will break your code.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
