2011-07-01 15 views
1

我有需要設置的項目範圍元標記。 我已將它們置於Bootstrap類的受保護方法_initMeta中。 有沒有更好的選擇?如果我想爲其他語言設置不同的這些數據呢?將meta標籤,鏈接和樣式放置在zend框架中的最佳實踐?

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'); 
} 

回答

2

我使用的基本(自舉)的數據作爲配置:

應用。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" 
... 

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

project1.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" 

最後,你混的都在你

bootstrap.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)); 
} 

然後,你可以覆蓋你的控制器的這些數據:setName,set ...

我希望它有幫助;)

2

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

話雖如此,除了引導,你可以設置的元數據上:

  • Front Controller Plugin類(使用例如dispatchLoopStartup()在preDispatch()方法方法)。
  • Action Helper類(例如使用init()preDispatch()方法)。

在這些執行點上,您可能已經確定了要使用的語言,並且可以相應地設置元數據。您可以隨時在action controllers中爲您的應用程序中的特定情況更改元數據,因此如果您之前指定了元數據,則永遠不會卡住。

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

  1. 前端控制器插件,dispatchLoopStartup()在方法確定語言加載,在請求一個「郎」 GET參數優先對象,然後是瀏覽器語言,然後是默認網站語言。我也用它來確定請求是一個正常請求還是一個ajax請求;如果是前者的話,我註冊一個動作助手,看下...
  2. 動作助手,preDispatch()方法方法:根據語言和其他的東西,負載Layout和部件負荷元數據等
  3. 動作控制器,someAction()方法:如有必要,更改一些先前設置的元數據,例如headTitle(),這可能取決於有效加載的內容。

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

0

bootstrap是我的項目早期的方式。我將它們添加到我的控制器/動作中

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

其實很晚很快就結束了。在這一點上,我也會知道語言和其他事情。