I'm trying to execute an action from the view using the Action helper like but although the action is been executed the output isn't displayed.
Here's part of my .phtml file:
<div id="active-users">
<?php echo $this->action('active', 'Users') ?>
</div>
The action works like this:
class UsersController extends Zend_Controller_Action
{
function activeAction()
{
$model = new UsersModel();
$this->view->users = $model->getActiveUsers();
}
}
And there's another .phtml file that renders the list of users. The action works fine when called directly from /users/active but doesn't display anything when called from inside another .phtml file.
I've tracked the problem to the ViewRenderer not been available when called with action() helper... or at least not working as usual (automatically rendering the default .phtml file).
The content is displayed if I explicitly render the view inside the action but I need the ViewRender behaviour because I don't control the code of some of the actions I need to use.
Is there anyway to turn the ViewRenderer on while using the action() view helper? I'm open to replace the action() view helper if needed.
I forgot: I'm using PHP 5.2.8, Zend Framework 1.7.5, Apache 2.2 on Windows Vista.
Thanks
i think you should asign the active users from the controller or if you want you can use singleton on the models an use the directly in the views
$this->view = UsersModel::instance()->getActiveUsers();
Are you using _forward() o redirect on your action? Actions that result in a _forward() or redirect are considered invalid, and will return an empty string.
Update: I test it, and it works, try writing 'users' instead of 'Users' in the controllers param.
<div id="active-users">
<?php echo $this->action('active', 'users') ?>
</div>
Related
I want to show some customized content in the left panel which usually contains the treeview.
As the stuff in this panel will be an editable Gridfield wich should be related to the EditForm, I tried to built a new EditFormTools panel in this way:
I copied the CMSMain_Content.ss in mysite/templates/Includes and changed $Tools into $EditFormTools
I created the file CMSMain_EditFormTools.ss in the same directory with this code:
<div class="cms-content-tools west cms-panel" data-expandOnClick="true" data-layout-type="border" id="cms-content-tools-CMSMain">
<div class="cms-panel-content west">
<% include Test %>
</div>
</div>
I created a Test.php with:
class Test extends CMSMain{
public $var = 'test';
public function testfunction(){
$variable = 'hakuna matata';
return $variable;
}
}
Then I created a Test.ss with this code:
some Text
$var
$testfunction
$variable
The Panel appears in my CMS now but it only contains "some Text". So the include of Test.ss works perfectly fine but passing variables from Test.php to Test.ss doesn't.
Can anybody help?
Greetings
It may not directly answer your question but may give you direction.
You need to extend a controller class.
Then you can use a called functions to tell controller which template file it should use using renderWith().
for example,
public function index(){
return $this->renderWith("Test");
}
Then your function references in Test.ss will call functions in Test.php given it is the controller.
If Test class is not the controller rendering the template, the template doesnt know where your variable returning function is.
By the way, you can pass variables from layout to include template.
I have a basic front-end form within a component that I created using ComponentCreator.
How and where do I direct the form "action"? Where can I handle this so that I am following the MVC design pattern? My form is within a view (default.php)
The form should simply insert to the database upon submission. I don't want to use a form building extension, I have tried these and they don't meet my requirements.
What version of Joomla? Assuming your not using AJAX then just direct to your controller.
<form id='MyForm' method='post' action='index.php?option=com_mycomponent&task=myoperation'>
<input type='text' name='myname'/>
...</form>
Then you will have a function in your controller (controller.php) called myoperation that will process all the things in your form (not forgetting the token of course)
JSession::checkToken('request') or jexit( JText::_( 'JINVALID_TOKEN' ) );
$jinput = JFactory::getApplication()->input;
$add_name = $jinput->get('myname', '', 'STRING');
//whatever you need to do...
//add to model if you wanted to add name to DB
$model = &$this->getModel();
$model->updateDB(...);
Then create function updateDB in model.
This is a rough example with Joomla 2.5 but should work with 3.x. Hope this helps.
Zend Framework 1.12
I have written my own view helper and need to call a Zend view helper from within it.
In my view file, I can call
$this->formSelect (...) to get a select dropdown
however in my own view helper file
$this->view->formSelect (...)
causes an error
Call to undefined method Zend_View_Helper_MilestoneList::formSelect()
How can I access Zend Framework view helpers from within there?
It is very simple to call another View Helper.
Your view helper extends must be extend Zend_View_Helper_Abstract, so that it has access to the $view. Then you may simply call helpers as you would from a view, i.e.
$this->view->generalFunctions()->progressMeter();
For example you can access it in to your view:
<?php
class Zend_View_Helper_FormVars extends Zend_View_Helper_Abstract {
/* ... */
public function mkCategoryCodeSelectGroup($codeTypeArr=array(),
$codesArr=array()) {
$html='';
$html. $this->view->generalFunctions()->progressMeter();
return $html;
}
}
Please set class name as per your need. and just try it.
let me know if i can help you
I found that
$selectFormHelper = $this->view->getHelper('FormSelect');
$selectFormHelper->formSelect(...)
works, but
$this->view->formSelect(...)
does not.
I'm not sure why that is, but happy to live with it for now.
Let's say I load a value from a database which return something like:
<?php
//Zend_Controller_Action
public function indexAction()
{
$dbContent = "<p>Hello <?php echo $user?>!</p>";
$this->view->paragraph = $dbContent;
}
?>
How is it possible, that
<?php echo $user?>
will be rendered?
What precaution need to be taken (safety issuses, XXS)?
Thanks so much indeed!
== Edit: ==
Sorry, I obviously formulated my question misunderstandingly. What I actually ment:
I would like to avoid implementing a template engine like smarty.
In my project, there will be content that has PHP-Code within a string and that needs to be parsed.
Example:
<?php
//Zend_Controller_Action
public function indexAction()
{
$dbContent = "<p>Hello <?php echo $user?>!</p>";
$this->view->paragraph = $dbContent;
}
<?php
//viewscript.phtml
$user = 'John Doe';
echo $this->paragraph;
?>
is supposed to output:
Hello John Doe!
Is there any safe solution to do this without an external template engine?
Thanks once more... :-)
If found a solution here, which seems to perfectly fill my needs.
Thanks to all who answered here,
==UPDATE==
Unfortunately the posted link is dead. However, the solution was pretty simple. As far as i Remember, it went through the following steps:
Fetch content from database and save it in a file
Use Zend_Cache to check, whether this file exists
If file exists, simply render it. If not, go to step 1.
==UPDATE II ==
Found a copy of the page:
archive.org
In zend framework you will be always be able to print string (or whatever you want) from a controller but it's a very bad practice.
You should give the $user value from the controller to the view in this way:
$this->view->paragraph = $user;
and then, in the view, have:
<p>Hello <?php echo $this->paragraph; ?>!</p>
To ensure this code from XSS you should do some check before (before you print the value) like this:
$user = strip_tags($user);
Zend Framework doesn't support automatic output escaping , but you can prevent XSS in many ways.
First of all push all values into view layer and then print them with a View Helper like Zend\View\Escape , by default it returns string under htmlspecialchars() but you can set a callback function simple with :
//view.phtml
$this->setEscape('yourClass','methodName');
$this->setEscape('functionName');
echo $this->escape($this->myGreatDbValue);
Sure you can create your custom View Helper for all your need.
Another way is to create a custom View class extending Zend\View\Abstract , override __get() magic method and filtering output .
Read documentations for Zend View Helper and Zend Filter: http://framework.zend.com/manual/en/zend.filter.html
http://framework.zend.com/manual/en/zend.view.helpers.html
What is the easiest way to categorise (warning, success, error) flash messages in Zend Framework using the FlashMessenger helper? I also want a single method to check for messages where the controller may not necessarily have forward the request on. At the moment, I believe this is done via FlashMessenger::getCurrentMessage()?
In you're controller you can do this :
$this->_helper->FlashMessenger(
array('error' => 'There was a problem with your form submission.')
);
$this->_helper->FlashMessenger(
array('notice' => 'Notice you forgot to input smth.')
);
In you're view you can echo the notice like this :
<?php echo $this->flashMessenger('notice'); ?>
And the error like this :
<?php echo $this->flashMessenger('error'); ?>
Edit:
Check this link :
... Calling the regular getMessages() method here won't work. This only returns messages which were stored in the appropriate ZendSession namespace when the FlashMessenger was instantiated. Since any messages added this request were not in the ZendSession namespace at that time (because the FlashMessenger was instantiated in order to add the messages) they won't be returned by getMessages().
For just this use-case, the FlashMessenger also provides a getCurrentMessages() method (and a related family of current methods) which returns those messages set on the current request.
Okay, thanks for everyone's input I have however implemented a different approach.
I already had a parent controller that extends Zend_Controller_Action where I've placed common logic across the application, so in the postDispatch() method I merged the getCurrentMessages and getMessages into a view variable.
public function postDispatch()
{
$messages = array_merge(
$this->_helper->flashMessenger->getCurrentMessages(),
$this->_helper->flashMessenger->getMessages()
);
$this->view->messages = count($messages) > 0 ? $messages[0] : array();
}
I set the message via a controller action like;
$this->_helper->flashMessenger(array('error'=>'This is an error'));
And in my layout file, I use a conditional on the $messages variable;
<?php if(count($this->messages) > 0) : ?>
//.. my HTML e.g. key($this->messages) returns 'error'
// current($this->messages) returns 'This is an error'
<?php endif; ?>
This works for me as the messages is categorised and can be obtained from the current request in addition to the next redirect.
Two ideas.
1. PHPPlaneta
Check out the source code of PHPlaneta by Robert Basic:
https://github.com/robertbasic/phpplaneta
He uses the standard FlashMessenger action helper:
$this->_helper->flashMessenger()->addMessage(array('fm-bad' => 'Error occurred')
Then defines a view helper called FlashMessenger so that he can access the messages. In his layout or view script, he simply calls:
<?php echo $this->flashMessenger(); ?>
The view helper uses the key (ex: 'fm-bad') to set up CSS styling for the output message.
2. PriorityMessenger
Check out the Priority Messenger view helper from Sean P. O. MacCath-Moran:
http://emanaton.com/code/php/zendprioritymessenger
The thing I like about this is that this whole business of saving messages for display on the next page load strikes me as something that should be completely within the view. So in your action, before your redirect, you populate the view helper with your messages and your priorities. Then, in your layout or view script, you output those messages with their priorities via the same view helper.