Theres a view helper called Action. Take a look at that..
(Might only be available in svn version, not sure.)
Hello guys, i'm a newbie here, now i am starting to learn about zend framework. And for now i know the basics on doing the framework. But i have a problem I'm trying to include a page into the main page. just like what include_once or require once do.
I have another controller for news and it already works under "news" but i would like to embed or include it into the index or main page. i try to use the $this->render('news/index.phtml');
but i got an error where i display the list on the news page...
Is there a way how to do it? hope you can help and share to me. thanks.
Theres a view helper called Action. Take a look at that..
(Might only be available in svn version, not sure.)
Sorry! but i didn't get your point, i'm using the ZF 1.0.3 i guess the latest one. but i'm not very familiar with all the libraries available in there...
Below is a copy of the view helper Action
(this one is taken from svn I think. Ie it might be outdated or otherwise flawed).
View helpers are like widgets/tools you can call from your view scripts. This particular one will call an action and return its output. So in order to include the output from the index action in the news controller you would write:
[PHP]
$this->Action('index', 'news');
//call an action with the parameter id set to 5
$this->Action('index', 'news', null, array('id'=>5));
//call an action in a different module (using mvc)
$this->Action('index', 'news', 'mymodule');
[/PHP]
View helpers should be put in the views/helpers/ dir.
[PHP]
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_View
* @subpackage Helper
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id: Action.php 6583 2007-10-04 18:39:07Z matthew $
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Helper for rendering output of a controller action
*
* @package Zend_View
* @subpackage Helpers
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_View_Helper_Action
{
/**
* @var string
*/
public $defaultModule;
/**
* @var Zend_Controller_Dispatcher_Interface
*/
public $dispatcher;
/**
* @var Zend_Controller_Request_Abstract
*/
public $request;
/**
* @var Zend_Controller_Response_Abstract
*/
public $response;
/**
* Constructor
*
* Grab local copies of various MVC objects
*
* @return void
*/
public function __construct()
{
$front = Zend_Controller_Front::getInstance();
$modules = $front->getControllerDirectory();
if (empty($modules)) {
require_once 'Zend/View/Exception.php';
throw new Zend_View_Exception('Action helper depends on valid front controller instance');
}
$request = $front->getRequest();
$response = $front->getResponse();
if (empty($request) || empty($response)) {
require_once 'Zend/View/Exception.php';
throw new Zend_View_Exception('Action view helper requires both a registered request and response object in the front controller instance');
}
$this->request = clone $request;
$this->response = clone $response;
$this->dispatcher = clone $front->getDispatcher();
$this->defaultModule = $front->getDefaultModule();
}
/**
* Reset object states
*
* @return void
*/
public function resetObjects()
{
$params = $this->request->getUserParams();
foreach (array_keys($params) as $key) {
$this->request->setParam($key, null);
}
$this->response->clearBody();
$this->response->clearHeaders()
->clearRawHeaders();
}
/**
* Retrieve rendered contents of a controller action
*
* If the action results in a forward or redirect, returns empty string.
*
* @param string $action
* @param string $controller
* @param string $module Defaults to default module
* @param array $params
* @return string
*/
public function action($action, $controller, $module = null, array $params = array())
{
$this->resetObjects();
if (null === $module) {
$module = $this->defaultModule;
}
$this->request->setParams($params)
->setModuleName($module)
->setControllerName($controller)
->setActionName($action)
->setDispatched(true);
$this->dispatcher->dispatch($this->request, $this->response);
if (!$this->request->isDispatched()
|| $this->response->isRedirect())
{
// forwards and redirects render nothing
return '';
}
return $this->response->getBody();
}
}
[/PHP]
Thank you so much for your sharing... sorry for being such noob here.
Anyway i try to browse on the library on the ZF i didn't find this Action.php under the View helper. or maybe i will add this class file to the library.
Other thing is regarding the sample on how to do it. Can you tell me where will i implement it, is it in the IndexController.php? I hope you can help me..
Thank you very much one again...![]()
Just add the action helper in your zend/view/helpers dir. It should be there by default in the next version (unless they come up with something new to replace it).
You put the call to action() in your view scripts. For instance in index.phtml add
<?php echo $this->action('index','news');?>
I'm happy to help!
It's works now with me... Thank you so much great help... Hopefully you can help me again, if i have other difficulties... Thanks once again![]()
Hi,All
I experienced the problem while using Zend_View_Helper_Action. I want to do the same thing as Weblizzer wanted.I included this Action.php file in Zend/View/Helper/.
I've explained the detail below.
I want to include the output of the XAction to YAction as include(),require() function does in php.But when I make a call to the <?php $this->Action(XAction,"my controller");?> in YAction.phtml and what all I get is my apache gets error message that says "The connection was reset...The connection to the server was reset while the page was loading.".
Can anyone tell me what's going wrong with me...
Thanks in advance...
That particular error is usualy caused by the php session timing out. But in this case I can't really say until you show us more of your code.
From your post I suspect that you are trying to use a view helper as if it was a controller, something that will always fail.
Post the relevant code here and I'll try to give you a better answer.
Hi,
Well,I wrote this in my bootstrap file.
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
//module
$frontController->addModuleDirectory(APP_DIR . DIRECTORY_SEPARATOR . 'modules');//APP_DIR . is constant
//Routing
$router = $frontController->getRouter()->addRoute(
'auth',
new Zend_Controller_Router_Route(':module/:controller/:action/*',
array('module' => 'default','controller'=>"index","action"=>"index") )
);
//Use the custom RenderHelper
$viewRenderer = new Custom_Controller_Action_Helper_ViewRenderer();
$view = new Zend_View();
$view->setScriptPath(array(
APP_DIR . DS . 'XXX-Dir'
));
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($vi ewRenderer);
====
and in application/modules/default/controllers/indexController.php has
<?php
class indexController extend zend_controller_action
{
public function indexAction()
{
//empty
}
}
corresponding template is located at application/XXX-Dir/index.tpl.php(!=.phtml) has code
<h1>Test</h1>
<?php echo $this->Action('test','test');?>
That calls the TestController.php and respective .tpl.php file followed by the above directory structure.
I've kept the Action.php file in lib/Zend/View/Helper/.
plz tell me what's wrong with here.
Sach