1

我有需要设置的项目范围元标记。我已将它们放在课堂上的受保护方法_initMetaBootstrap。有没有更好的选择?如果我想要其他语言的不同数据集怎么办?

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

3 回答 3

2

您有几种方法可以实现这一目标。首先,确保在定义元数据的那一刻,您已经知道将为当前请求加载哪种语言。有时这在引导时可能不容易确定。

话虽如此,除了引导程序之外,您还可以在以下位置设置元数据:

在这些执行点上,您可能已经确定了要使用的语言,并且可以相应地设置元数据。之后,您始终可以针对应用程序中的特定情况在动作控制器中更改元数据,因此如果您之前指定了元数据,您永远不会真正陷入困境。

在我自己的工作中,我有这样的设置:

  1. 前端控制器插件,dispatchLoopStartup() 方法确定要加载的语言,优先考虑请求对象中的“lang”GET 参数,然后是浏览器语言,然后是默认站点语言。我也用它来判断请求是普通请求还是ajax请求;如果是前一种情况,我注册一个动作助手,看下一个......
  2. Action Helper,preDispatch() 方法: 根据语言和其他内容加载元数据,加载布局和小部件等。
  3. 动作控制器, someAction() 方法:如有必要,更改一些先前设置的元数据,例如headTitle(),这可能取决于有效加载的内容。

这种安排对我来说很有意义,也许它可以帮助你的方法?

于 2011-07-01T19:38:04.727 回答
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"
...

在这一点上,也许你有一些特殊的数据。例如在:

项目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...

我希望它有帮助;)

于 2012-11-03T04:20:17.070 回答
0

引导程序对于我的项目来说是早期的方式。我将它们添加到我的控制器/操作中

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

实际上到最后几乎很晚了。在这一点上,我也会知道语言和其他事情。

于 2011-07-01T20:58:36.360 回答