Clear render_PHP tutorial in php Zend Framework

WBOY
Release: 2016-07-21 14:56:23
Original
1080 people have browsed it

I became interested in php mvc when using asp.net mvc. I looked at Zend Framework (hereinafter referred to as ZF) and I can compare and understand mvc. I was confused about a relatively core function render for a while. Record the clarification process

Usually when we use ZF to implement PHP's mvc, the most critical place is of course the various action methods of the Controller class. In the action method, we determine and output the content. In the dispatch method in the abstract class Zend_Controller_Action, you can Found this line $this->$action();

Then how to determine and output the content is to render, but there are several renders. These situations are listed below

1 2class IndexController extends Zend_Controller_Action
3{
4 public function contactAction()
5 {
6 //$this->render("index");
7 //$this->render();
8 //$this->renderScript("sidebar.phtml");
9
10
11 //$this->_helper->viewRenderer("sidebar");
12
13 //$this->view->render("sidebar.phtml");
14 //$this->view("sidebar");
15
16}
17}
18?>

To sum up, it seems that these are the three renders (welcome additions)

1. Self render

Look at the first type first
//$this->render("index");
//$this->render();
//$this->renderScript("sidebar.phtml");
This is directly using the render method of the Zend_Controller_Action class
The first sentence is to render the view corresponding to another action (see clearly that it is to render the view corresponding to that action, not to execute that action!)
The second sentence renders the view corresponding to this action. What is the meaning of this (because in many cases you cannot see this writing), we will discuss this below.
The third sentence is a render-specific view file. Here you may think that the first two methods actually call this renderScript, but this is not the case.
Let’s explain it below. By the way, explain the reason for the second sentence.
There are actually two branches in the render method of the Zend_Controller_Action class. The render function code is as follows

1 public function render($action = null, $name = null, $noController = false)
2 {
3 if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
4 return $this->_helper->viewRenderer->render($action, $name, $noController);
5}
6
7 $view = $this->initView();
8 $script = $this->getViewScript($action, $noController);
9
10 $this->getResponse()->appendBody(
11 $view->render($script),
12 $name
13);
14}
You can see that one situation is to use (proxy) the render method of the view assistant class (viewRenderer)
The other situation is when the assistant is disabled, you have to do it yourself. This is why render() appears. After you disable the view assistant, you can use render() to output the view content corresponding to this action.

2. Through the view assistant viewRenderer

We talked about the view assistant above, let’s look at the second fragment in the action, which is done with the help of the view assistant
//$this->_helper->viewRenderer("sidebar");
In fact, this sentence here is not the render content, but specifies which view to render. Refer to this function of the Zend_Controller_Action_Helper_ViewRenderer class

1 public function direct($action = null, $name = null, $noController = null)
2 {
3 $this->setRender($action, $name, $noController);
4}
So what about the output? How is it output?
You can call $this->render(); directly after $this->_helper->viewRenderer("sidebar");.
But actually you don’t need to call it at all, just write that sentence.
When you don’t write render, the view assistant will do it for you. There is such a sentence in the dispatch method in the Zend_Controller_Action class
$this->_helper->notifyPostDispatch();
What is _helper? It is a Zend_Controller_Action_HelperBroker class, which has this method

1 public function notifyPostDispatch()
2 {
3 foreach (self::getStack() as $helper) {
4 $helper->postDispatch();
5}
6}

You can see that postDispatch() of each assistant is called;
The viewRenderer is one of its assistants, and its postDispatch method is as follows

1 public function postDispatch()
2 {
3 if ($this->_shouldRender()) {
4 $this->render();
5}
6}
It is here that the view assistant helps you render. If you render it yourself, the smart view assistant will know it. You can check the $this->getRequest()->isDispatched() in _shouldRender() ), and this sentence of the dispatch method in the Zend_Controller_Front class: $this->_request->setDispatched(true);

  • Total 2 pages:
  • Previous page
  • 1
  • 2
  • Next page

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364271.htmlTechArticleAsp.net mvc, I became interested in php mvc and took a look at Zend Framework (hereinafter referred to as ZF). I can compare and understand mvc. I was very confused about a relatively core function render for a while and recorded the clear process usually...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template