ZendFramework 2 - 你如何从控制器处理ZF1中的布局头部页脚?

时间:2012-10-23 08:09:33

标签: php zend-framework zend-form zend-framework2 zend-controller

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

我已经通过ommiting - >查看调用尝试了$this->headTitle('..');,但它仍然因为抛出而失败:

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 个答案:

答案 0 :(得分:5)

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

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

    return new ViewModel();
}

答案 1 :(得分:1)

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

public function indexAction(){
  $this->headTitle("My website")->setSeparator(" - ")->append("easy ?!");
  return new ViewModel();
}

答案 2 :(得分:1)

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

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
}

答案 3 :(得分:0)

为所有模块编写插件

broker.brokerCodeField

在模块配置中附加插件

<select ng-model="equiriesSelectedBroker" class="form-control" 
ng-options="broker.userIdField as broker.brokerCodeField for broker in enquiriesBrokers" id="enquiries-broker">
    <option value="">Please select a broker</option>
</select>

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

class HeadTitlePlugin extends AbstractPlugin
{

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

多数人