How to change meta tags in zend framework layout

你。 提交于 2019-12-02 19:39:39

I would recommend setting a view variable for the keywords. For example, in your bootstrap.php you could define a default keywords as follows:

protected function _initSetDefaultKeywords() {
     $view = $this->bootstrap('view')->getResource('view');        
     $view->keywords = 'default keywords';
}

In layout.phtml you would have then:

<?php echo $this->headMeta()->appendName('keywords', $this->keywords); ?>

Finally, in your views (or actions) you could change the meta keywords simply by changing the keywords view variable:

// in an action
$this->view->keywords = 'new kewords';
//or in a view
<?php $this->keywords = 'new kewords'; ?>

I just tested this, and setName() should work:

<?php $this->headMeta()->setName('keywords', 'test'); ?>
<?php $this->headMeta()->setName('keywords', 'test'); ?>

Results in:

<meta name="keywords" content="another test" >  

While:

<?php $this->headMeta()->setName('keywords', 'test'); ?>
<?php $this->headMeta()->appendName('keywords', 'another test'); ?>

Results in:

<meta name="keywords" content="test" > 
<meta name="keywords" content="another test" > 

Here's what worked for me. In the layout file you have to make sure that the meta tags are echoed. It's empty at the stage but you will be marking where the meta tags will be outputted. This only disadvantage with this method would be that there doesn't seem to be a way to have a default meta tag so you will have to add the meta tag in each view file.

In the layout file

<?php echo $this->headMeta(); ?>

In the view .phtml file

$this->headMeta("test description text", "description");

keywords and description generating without controller/action

register 2 plugin in bootstrap

    // register navigation, $view->navigation()->setContainer( new Zend_Navigation( $navigationArray ) );
    $controller = Zend_Controller_Front::getInstance();
    $controller->registerPlugin(new App_Controller_Plugin_PrepareNavigation());

    $controller->registerPlugin(new App_Controller_Plugin_SetMeta());

Meta plugin could look like:

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
        $activePage = $view->navigation()->findOneBy('active', true);

        $view->headTitle($activePage->title);
        $view->headMeta()->appendName('keywords', $activePage->keywords );
        $view->headMeta()->appendName('description', $activePage->description );
        $view->pageHeader = $activePage->pageHeader;
    }

navigationArray would be than:

          'pages' => array(
            array( 'label'  => 'introduction',
                   'controller' => 'controller',
                   'action' => 'introduction',
                   'route'  => 'controlleraction',
                   'pageHeader' => 'h1 or something',
                   'title' => 'used as meta title'
                   'keywords' => 'meta keywords'
                   'description' => 'meta desc',

than you can simple call (from layout/view) print $this->pageHeader;

Matt Setter

How about:

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

I found this, which works for me, on Best practice to place meta tags, links and styles in zend framework?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!