簡體   English   中英

在 zend 框架中放置元標記、鏈接和 styles 的最佳實踐?

[英]Best practice to place meta tags, links and styles in zend framework?

我有需要設置的項目范圍元標記。 我已將它們放在Bootstrap class 中的受保護方法_initMeta中。 有沒有更好的選擇? 如果我想要其他語言的不同數據集怎么辦?

protected function _initMeta(){
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->doctype('XHTML1_STRICT');

    $view->headTitle()->headTitle('Foo title');

    $view->headMeta()->appendName('keywords','foo');

    $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8')
            ->appendHttpEquiv('Content-Language', 'any');

    $view->headLink()->appendStylesheet('/foo.css')->headLink(array('rel' => 'favicon',
                              'href' => '/favicon.ico'),
                              'PREPEND');
}

我將配置用於基本(引導)數據:

應用程序.ini

resources.view.meta.name.Viewport                       = "width=device-width, initial-scale=1.0"
resources.view.meta.name.MobileOptimized                    = "width"
resources.view.meta.name.HandheldFriendly                   = "true"
resources.view.meta.name.Keywords                       = "basic,keywords"
...
; format resources.view.headStyle.{MEDIA}.nfile = 
resources.view.headStyle.all.1.href                 = "/css/basic.css"
resources.view.headStyle.all.1.conditionalStylesheet            = 
resources.view.headStyle.all.1.extras.title             = "Basic style"
resources.view.headStyle.all.1.extras.charset               = "utf-8"

resources.view.headStyle.all.2.href                 = "/css/ie.css"
resources.view.headStyle.all.2.conditionalStylesheet            = "IE"
resources.view.headStyle.all.2.extras.title             = "Internet Explorer style"
resources.view.headStyle.all.2.extras.charset               = "utf-8"
; print media example
resources.view.headStyle.print.1.href                   = "/css/print.css"
...
; format resources.view.headLink.{REL} = 
resources.view.headLink.humans.href                     = "/humans.txt"
resources.view.headLink.humans.type                     = "text/plain"
; ___ will be replaced by space, __ by point (or set another nest separator)
resources.view.headLink.shortcut___icon.href                = "/favicon.png"
resources.view.headLink.shortcut___icon.type                = "image/png"
...

在這一點上,也許你有一些特殊的數據。 例如在:

項目1.ini

project.headLink.author.href                = "https://plus.google.com/XXXXX?rel=author"
project.headLink.image_src.href                     = "/author.jpg"
project.headLink.image_src.type                     = "image/jpg"

最后,你把所有的東西都混合在你的

引導程序.php

(*_initHeadLink()* 的示例):

// $options = your app options (basic)
// $projectOptions = your project options (special)
// $assets_url = your assets url

if ( is_array($headStyle = $options['headStyle']) ) {
    foreach ( $headStyle as $media => $value ) {
        foreach ( $value as $style ) {
            extract($style);
            $this->view->headLink()->appendStylesheet($assets_url . $href, $media, 
                            $conditionalStylesheet, $extras);
        }
    }
}

$headLinks      = array();

if ( isset($options['headLink']) ) 
    $headLinks      = $options['headLink'];

if ( isset($projectOptions['headLink']) ) 
    $headLinks      = array_merge($headLinks, (array) $projectOptions['headLink']);

// *array key, is the value for rel
foreach ( $headLinks as $rel => $value ) {
    $rel            = str_replace(array('___', '__'), array(' ', '.'), $rel);
    $this->view->headLink()->headLink(array_merge(array('rel' => $rel), (array) $value));
}

然后,您可以從 Controller 中覆蓋這些數據:setName, set...

我希望它有所幫助;)

您有幾種方法可以實現這一目標。 首先,確保在定義元數據的那一刻,您已經知道將為當前請求加載哪種語言。 有時這在引導時可能不容易確定。

話雖如此,除了引導程序之外,您還可以在以下位置設置元數據:

在這些執行點上,您可能已經確定了要使用的語言,並且可以相應地設置元數據。 之后,您始終可以針對應用程序中的特定情況在動作控制器中更改元數據,因此如果您之前指定了元數據,您永遠不會真正陷入困境。

在我自己的工作中,我有這樣的設置:

  1. 前面 Controller 插件,dispatchLoopStartup() 方法確定要加載的語言,優先考慮請求 object 中的“lang”GET 參數,然后是瀏覽器語言,然后是默認站點語言。 我也用這個來判斷請求是普通請求還是ajax請求; 如果是前一種情況,我注冊一個動作助手,看下一個......
  2. Action Helper,preDispatch() 方法:根據語言和其他內容加載元數據,加載布局和小部件等。
  3. Action controller, someAction() 方法:如有必要,更改一些先前設置的元數據,例如headTitle() ,這可能取決於有效加載的內容。

這種安排對我來說很有意義,也許它可以幫助你的方法?

引導程序對於我的項目來說是早期的方式。 我將它們添加到我的控制器/操作中

$keywords = 'php,zend,framework';
$this->view->headMeta($keywords,'keywords','name',array(),'SET');
... etc.

實際上到最后幾乎很晚了。 在這一點上,我也會知道語言和其他事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM