The render usage in PHP Zend framework is introduced in detail.

Source: Internet
Author: User
Tags zend zend framework

http://www.pkphp.com/2010/01/09/zend-framework-render-intro/


Often 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 which we identify and output the content. Dispatch method in class zend_controller_action you can find this line $this-> $action ();

So how to determine and output the content, is to carry out the render, but the render is there are several, the following list of these situations
<?php
Class Indexcontroller extends Zend_controller_action
{
Public Function contactaction ()
{
$this->render ("index");
$this->render ();
$this->renderscript ("sidebar.phtml");


$this->_helper->viewrenderer ("sidebar");

$this->view->render ("sidebar.phtml");
$this->view ("sidebar");

}
}
?>

Summed up, it seems that the three render (Welcome to add)


1. Self Render

First of all, look.
$this->render ("index");
$this->render ();
$this->renderscript ("sidebar.phtml");
This is the render method for directly using the Zend_controller_action class.
The first sentence is render the corresponding view of the other action (see the view of the render action instead of executing that action!)
The second sentence render the corresponding view of this action, what is the meaning of this (because many situations you do not see this writing), this next.
The third sentence is render a particular view file, where you might think the first two methods actually call this renderscript, but that's not the case.
Here is the explanation. By the way, explain the second sentence.
The Render method of the Zend_controller_action class actually has two branches of the following render function code
Public function render ($action = null, $name = NULL, $noController = False)
{
if (! $this->getinvokearg (' noviewrenderer ') && $this->_helper->hashelper (' Viewrenderer ')) {
return $this->_helper->viewrenderer->render ($action, $name, $noController);
}

$view = $this->initview ();
$script = $this->getviewscript ($action, $noController);

$this->getresponse ()->appendbody (
$view->render ($script),
$name
);
}
You can see a situation where the Render method of the View Helper Class (Viewrenderer) is exploited (by proxy)
The other is to disable the assistant when the situation has to go in person, this is the reason for render (), you disable the View Assistant to output this action corresponding view content can use render () to complete

2. Through the View assistant Viewrenderer

The view Assistant is mentioned above, so let's look at the second fragment in the action, which is done using the View assistant
$this->_helper->viewrenderer ("sidebar");
In fact, this sentence is not render content, but specifies which view to render, reference Zend_controller_action_helper_viewrenderer class of this function
Public Function Direct ($action = null, $name = NULL, $noController = NULL)
{
$this->setrender ($action, $name, $noController);
}
So how does the output output?
Can be in $this->_helper->viewrenderer ("sidebar"); Immediately after the call $this->render ();
But in fact you don't have to call at all, just write that sentence on the line.
When you don't write render, the view assistant will come and do it for you. In the dispatch method in the Zend_controller_action class, there is a sentence
$this->_helper->notifypostdispatch ();
What is _helper? is a Zend_controller_action_helperbroker class, which has this method
Public Function Notifypostdispatch ()
{
foreach (Self::getstack () as $helper) {
$helper->postdispatch ();
}
}

You can see the Postdispatch () that invoked each of the assistants;
And Viewrenderer is one of the assistants, the Postdispatch method is as follows
Public Function Postdispatch ()
{
if ($this->_shouldrender ()) {
$this->render ();
}
}
It is here that the View assistant helps you render, and if you render it, the Smart View Assistant will know and can look at this $this->getrequest () in _shouldrender ()->isdispatched (), and this sentence of the dispatch method in the Zend_controller_front class: $this->_request->setdispatched (TRUE);

3. Ultimate Render about Zend_view->render ()

Okay, now let's look at Zend_view's render ().
In the top two we all talked about render (), such as the action render and the view assistant's render
So you should ask a question: is that it?
The back is the key.
In the render of the action, you may have noticed this sentence.
$this->getresponse ()->appendbody (
$view->render ($script),
$name
);
And then we look at Viewrenderer's render (), Viewrenderer's Render method is to call the Renderscript method, the code is as follows
Public Function Renderscript ($script, $name = null)
{
if (null = = $name) {
$name = $this->getresponsesegment ();
}

$this->getresponse ()->appendbody (
$this->view->render ($script),
$name
);

$this->setnorender ();
}
You can see that there is something similar to the render of the action, and there is the same sentence.
That is, the action of the render and viewrenderer render is actually called Zend_view render, get the content and then placed into the response

Zend_view's Render:
Public function render ($name)
{
Find the script file name using the parent private method
$this->_file = $this->_script ($name);
Unset ($name); Remove $name from local scope

Ob_start ();
$this->_run ($this->_file);

return $this->_filter (Ob_get_clean ()); Filter output
}

As for run:
protected function _run ()
{
if ($this->_useviewstream && $this->usestreamwrapper ()) {
Include ' zend.view://'. Func_get_arg (0);
} else {
Include Func_get_arg (0);
}
}

Then you'll see the 13th line in the first code.
->view->render//$this ("sidebar.phtml");
Actually, it's a cover, huh. This sentence just got the content, but did not do the processing!
That's why we should.
echo $this->view->render ("sidebar.phtml");
And then what? Refer to Zend_controller_front class dispatch
$this->_response->sendresponse ();
and Zend_controller_response_abstract class
Public Function Outputbody ()
{
foreach ($this->_body as $content) {
Echo $content;
}
}

And as for line 14th
->view//$this ("sidebar");
Seemingly reasonable, stare at a glance will know: this sentence is a mistake
There is no such method in the action, and there is no corresponding processing in the __call, unlike _helper->viewrenderer ("sidebar"), which is handled in __call in _helper
Public Function __call ($method, $args)
{
$helper = $this->gethelper ($method);
if (!method_exists ($helper, ' direct ')) {
Require_once ' zend/controller/action/exception.php ';
throw new Zend_controller_action_exception (' Helper '. $method. ' does not support overloading via direct () ');
}
return Call_user_func_array (Array ($helper, ' direct '), $args);
}
Without Viewrenderer This method, we went to the assistant named Viewrenderer and the direct method, and found that the direct method was executed (the second part of the code was pasted).
As for Viewrenderer This assistant store, should notice his name is Dead is "viewrenderer", concrete look Zend_controller_action_helper_abstract class GetName method
Public Function GetName ()
{
$full _class_name = Get_class ($this);

if (Strpos ($full _class_name, ' _ ')!== false) {
$helper _name = STRRCHR ($full _class_name, ' _ ');
Return LTrim ($helper _name, ' _ ');
} else {
return $full _class_name;
}
}
This is because you can see this in the Zend_controller_action_helper_viewrenderer annotation.
In your action controller methods:
$viewHelper = $this->_helper->gethelper (' View ');
And the fact that you copy this sentence into your action controller methods will only make a mistake.
Exception Information:

Message:action Helper by name View not found

Personal feel PHP Zend framework is still very good, although the debugger has not been well
But very satisfied with the var_dump can be placed anywhere

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.