在 zend 框架中放置元标记、链接和样式的最佳实践?

发布于 2024-11-17 20:14:56 字数 764 浏览 4 评论 0原文

我有需要设置的项目范围元标记。 我已将它们放入 Bootstrap 类中的受保护方法 _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');
}

I have project-range meta tags that are need to be set.
I've put them in protected method _initMeta in Bootstrap class.
Are there any better options? What if I would like different set of this data for another languages?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2024-11-24 20:14:56

我使用基本(引导)数据的配置为:

application.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...

我希望它有所帮助; )

I use config for basic (bootstrap) data as:

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

At this point, maybe you have some special data. For example in:

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"

And finally, you mix all in your

Bootstrap.php

(example for *_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));
}

Then, you can override these data from your Controller: setName, set...

I hope it helps ;)

蓝海 2024-11-24 20:14:56

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

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

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

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

  1. 前端控制器插件,dispatchLoopStartup()方法确定要加载的语言,优先考虑请求中的“lang”GET参数对象,然后是浏览器语言,然后是默认站点语言。我也是用这个来判断请求是普通请求还是ajax请求;如果是前一种情况,我注册一个动作助手,请参阅下一步...
  2. 动作助手,preDispatch() 方法: 根据语言加载元数据和其他内容,加载< a href="http://framework.zend.com/manual/en/zend.layout.html" rel="nofollow">布局和小部件等
  3. Action 控制器,someAction() 方法:如果必要时,更改一些之前设置的元数据,例如 headTitle(),它可以依赖于有效加载的内容。

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

You have several ways of achieving this. First and foremost, make sure the moment you define the metadata you already know what language will be loaded for the current request. Sometimes this may not be easy to determine at bootstrap time.

Having said this, besides the bootstrap, you can set the metadata on a:

At those points of execution you probably already determined the language to use, and can set the metadata accordingly. You can always change the metadata afterwards in your action controllers for specific cases in your application, so you're never really stuck if you previously specified metadata.

In my own work, I have this setup:

  1. Front Controller Plugin, dispatchLoopStartup() method: determine language to load, giving priority to a "lang" GET parameter in the request object, then browser language, then default site language. I also use this to determine if the request is a normal request or an ajax request; if it's the former case, I register an action helper, see next...
  2. Action Helper, preDispatch() method: load metadata depending on language and other stuff, load Layout and widgets, etc.
  3. Action controller, someAction() method: if necessary, change some of the previously set metadata, for example headTitle(), which can depend on the effectively loaded content.

This arrangement makes sense to me, maybe it can help your approach?

雨轻弹 2024-11-24 20:14:56

对于我的项目来说,引导程序还为时过早。我将它们添加到我的控制器/操作中

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

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

The bootstrap is way to early for my projects. I add them in my controller/actions

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

Actually very late almost at the end. At this point I would also know about language and other things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文