0

如何以 ZF2 方式即时执行 ZF1 中显示的以下 __construct 部分?

我已经尝试$this->headTitle('..');通过省略 ->view 调用,但它仍然通过抛出失败:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for headTitle

  public function __construct() { //init is gone 

    $this->_helper->layout()->setLayout('brand');
    $this->HeadTitle($this->title)->setIndent(8);
    $this->view->headMeta()->appendName('keywords', $this->keyword)->setIndent(8);
    $this->view->headMeta()->appendName('description', $this->description)->setIndent(8);
    $this->view->headMeta()->appendName('Language', 'en')->setIndent(8);

    $this->view->headMeta()->appendName('dc.title', $this->title)->setIndent(8);
    $this->view->headMeta()->appendName('dc.keywords', $this->keyword)->setIndent(8);
    $this->view->headMeta()->appendName('dc.description', $this->description)->setIndent(8);

    $this->view->headLink()->appendStylesheet('/css/main.css')->setIndent(8);
    $this->view->headLink()->appendStylesheet('/jquery/css/custom-theme/jquery-ui-
1.8.20.custom.css')->setIndent(8);

    $post = $this->getRequest()->getPost();
    $get = $this->getRequest()->getQuery();
  }
4

4 回答 4

4

您可以在动作控制器中访问“渲染器”对象:

public function indexAction()
{
    $renderer = $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer');
    $renderer->headTitle('My title');

    return new ViewModel();
}
于 2012-10-23T09:11:12.037 回答
0

我遇到了同样的问题,我开发了一个 ZF2 插件来使用 headTitle,就像在 layout.phtml 文件中一样。 https://github.com/remithomas/rt-headtitle

public function indexAction(){
  $this->headTitle("My website")->setSeparator(" - ")->append("easy ?!");
  return new ViewModel();
}
于 2013-03-25T15:36:55.283 回答
0

编写一个函数来处理控制器中的所有操作

protected function setHeadTitle($title = ''){
    if(!empty($title)){            
        $renderer = $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer');
        $renderer->headTitle($title);
    }
}

在您的操作中使用该功能

public function loginAction()
{
    $this->setHeadTitle("Login");
    //write some other codes
}
于 2015-03-25T07:22:24.553 回答
0

为所有模块编写插件

class HeadTitlePlugin extends AbstractPlugin
{

    public function setHeadTitle($title = '')
    {
        if (! empty($title)) {
            $renderer = $this->getController()->getServiceLocator()->
                              get('Zend\View\Renderer\PhpRenderer');
            $renderer->headTitle($title);
        }
    }
}

在模块配置中附加插件

'controller_plugins' => array(
    'invokables' => array(
        'HeadTitlePlugin' => 'Modulename\Controller\Plugin\HeadTitlePlugin'
    )
),

在控制器动作中调用插件函数

public function indexAction()
{
    $this->HeadTitlePlugin()->setHeadTitle("Signup");
    // other codes 
}

就这样

于 2015-05-05T11:39:32.497 回答