phpzend-frameworkmeta-tagszend-app-bootstrap

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


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

Solution

  • 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 ;)