Zend Framework: How to pass data from bootstrap to layout? - zend-framework

I have some configuration values set in application.ini and i want to pass those values to the layout on application load. How can i do this from bootstrap ? For trial i tried doing this
In my Bootstrap initialization function:
$this->bootstrap('view');
$view = $this->getResource('view');
$view->layout()->whatever = "Some Value";
In layout:
<?php echo $this->layout()->whatever; ?>
But m not able to get the value to display in the layout.

The following should work:
$this->bootstrap('view');
$view = $this->getResource('view');
$view->whatever = 'Some value';
Then, in layout:
<?php echo $this->whatever ?>

You have to grab the Layout and from there the view object:
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->text = 'Welcome';

Related

Zend Framework addaction with success or failure message

public function addAction()
{
$form = new ApplicationForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$name = $form->getvalue('name');
$class = $form->getvalue('class');
$file = new Application_Model_DbTable_Records();
$file->addRecord($name,$class);
$this->_helper->redirector('index');
}
}
}
Above addAction controller part, here when i am clicking AddAction my form is waiting for user inputs when i click submit my inputs recorded in database.
Now my question is i want add some message after the submit form data whether it success or failure.
Could you please help me on this ?
Many Thanks,
viswa
The docs for the action-helper describe an example. But standard usage goes something like this:
After you add the record, before you redirect, set the desired message in your controller:
public function addAction()
{
$form = new ApplicationForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$name = $form->getValue('name');
$class = $form->getValue('class');
$file = new Application_Model_DbTable_Records();
$file->addRecord($name,$class);
// Add the message here
$this->_helper->getHelper('FlashMessenger')->addMessage('Record added');
$this->_helper->redirector('index');
}
}
}
Then in your indexAction - the controller to which you are redirecting after successful record addition - get the messages and add them to your view:
public function indexAction()
{
// All your existing processing
// Blah, blah..
// Get the messages from the FlashMessenger
$messenger = $this->_helper->getHelper('FlashMessenger');
$messages = $messenger->hasMessages() ? $messenger->getMessages() : [];
// Add the messages into the view
$this->view->messages = $messages;
}
Finally, somewhere in the index view-script where you want the messages to appear, check for the messages and render, something like:
<?php if ($this->messages): ?>
<div id="refresh-messages">
<ul>
<?php foreach ($this->messages as $message): ?>
<li><?= $message ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
The wrapping div is just to assist with styling by providing a DOM element id to which you can target your CSS.
Disclaimer: Not tested directly, just coding from memory.

zendframework with jquery UI datepicker getting future date 2019 instead of 2013

I get future date instead of 2013
below is the code
protected function _initView()
{
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headTitle()->setSeparator(' - ');
//$view->headTitle('IMR - BI System');
$view->env = APPLICATION_ENV;
//$view->baseUrl = Zend_Registry::get('config')->root_path;
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
// $view->jQuery()->addStylesheet($view->baseUrl . '/js/themes/base/demos.css');
$view->jQuery()->addStylesheet($view->baseUrl . '/js/themes/base/jquery.ui.all.css');
$view->jQuery()->addStylesheet($view->baseUrl . '/js/css/jquery.ui.theme.css');
$view->jQuery()->setLocalPath($view->baseUrl . '/js/jquery.min.js');
$view->jQuery()->setUiLocalPath($view->baseUrl .'/js/jquery-ui.min.js');
$view->jQuery()->enable();
$view->jQuery()->uiEnable();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
return $view;
}
In the view i gave
Pick your Date: <?php echo $this->datePicker("dp1", '', array('defaultDate' => date('Y/m/d', time()))); ?>
Have you tried simply not using the defaultDate parameter? It shoult default to the current date anyway. I have seen the same behaviour using defaultDate, but its fine if you dont use it.
try setting the format instead:
'dateFormat' => 'dd-mm-yy'

How to bootstrap Zend view just from application.ini?

I'm using Zend Framework 1.12 and I would like to bootstrap my view in the application.ini only.
Current setup:
resources.view[] = ""
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/"
resources.view.doctype = "XHTML1_STRICT"
resources.view.contentType = "text/html; charset=UTF-8"
resources.view.helperPath.My_View_Helper = APPLICATION_PATH"/views/helpers"
By this setup, I can use (for example) built-in navigation helper. I must attach following to Bootstrap:
protected function _initViewHelpers() {
$this->bootstrap('view');
$view = $this->getResource('view');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
$view->env = APPLICATION_ENV;
}
Any possibility to move it to ini file ?

Not getting the form data loaded into [log:Zend_View_Abstract:private]

I assigned
$this->view->form = $form (form instance) and now trying to display the content in layout.phtml using
echo $this->form;
but not displaying anything. I tried to check contents using
print_r($this);
and I see no data loaded into [log:Zend_View_Abstract:private].
Could anyone please help me on how to get data loaded in phtml file.
AuthForm.php
class forms_AuthForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('login');
$username = new Zend_Form_Element_Text('username','username');
$username->setLabel('Username:')
->setRequired(true)
->setOptions(array('class'=>'longfield'));
$password = new Zend_Form_Element_Text('password','password');
$password->setLabel('Password: *')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit','submit');
$submit->setLabel('Login');
$this->addElements(array($username, $password,$submit));
}
}
And AuthController.php
$form = new forms_AuthForm();
$this->view->form = $form;
Regrds
kiran
Iam not sure, but i think you have to assign the form to the layout not the view.
$layout = Zend_Layout::getMvcInstance();
$form = new forms_AuthForm();
$layout->form = $form;
and in your layout.phtml
echo $this->layout()->form;

ZendX date picker example not working

I am rather new to ZendX and I really wanted to get the simple JQuery example on Zend to get working.I have followed the example on the link below but all I get is a plain textbox without any datepicker functionality as I expected.
Best way to start using jQuery in a Zend Framework 1.9 application?
In my bootstrap I have
protected function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('JQUERY Test');
//assuming you already have this function in your bootstrap
//jQuery (using the ui-lightness theme)
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
$view->jQuery()->addStylesheet('/js/jquery/css/ui-lightness/jquery-ui-1.7.2.custom.css')
->setLocalPath('/js/jquery/js/jquery-1.3.2.min.js')
->setUiLocalPath('/js/jquery/js/jquery-ui-1.7.2.custom.min.js');
}
In my layout I have included
<head>
<?php echo $this->HeadMeta(); ?>
<?php echo $this->headTitle(); ?>
<?php echo $this->headLink(); ?>
<?php echo $this->headScript(); ?>
<?php echo $this->jQuery(); ?>
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/main.css'); ?>
<?php echo $this->render('_javascript.phtml'); ?>
</head>
What am I missing?
Did you call the view helper from
within your view script, with valid options? See example
from your refered question
Did you double check the paths to your local js- & css-files?
You add ZendX_JQuery::enableView($view); to _initViewHelpers
i go through application.ini way which works out like this:
resources.view.helperPath.ZendX_JQuery_View_Helper = "ZendX/JQuery/View/Helper"
resources.view[] =
pluginPaths.ZendX_Application_Resource = "ZendX/Application/Resource"
resources.jquery.localpath = "/project1/public/jquery/development-bundle/jquery-1.7.1.js"
resources.jquery.stylesheet = "/project1/public/jquery/development-bundle/themes/smoothness/jquery-ui-1.8.18.custom.css"
resources.jquery.uilocalpath = "/project1/public/jquery/development-bundle/ui/jquery-ui-1.8.18.custom.js"
I am not sure about bootstrap code but what I got from research is the below code. May be the last three lines will help.
protected function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('JQUERY Test');
//assuming you already have this function in your bootstrap
//jQuery (using the ui-lightness theme)
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
$view->jQuery()->addStylesheet('/js/jquery/css/ui-lightness/jquery-ui-1.7.2.custom.css')
->setLocalPath('/js/jquery/js/jquery-1.3.2.min.js')
->setUiLocalPath('/js/jquery/js/jquery-ui-1.7.2.custom.min.js');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
}