Zend pass variable from plugin to view - zend-framework

In plugin I try $this->view->menu = $menu; , but in view I try <?php var_dump($this->menu); ?> and get NULL
Maybe are solution to pass variable from plugin to view ?

you can use like below
First : Without using helpers or plugins do :
Zend_Layout::getMvcInstance()->assign('whatever', 'foo');
After this you can use the following in your layout:
<?php echo $this->layout()->whatever; ?>
This will print "foo".
OR
Second :
<?php
class My_Layout_Plugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->whatever = 'foo';
}
}
then register this plugin with the front controller, e.g.
Zend_Controller_Front::getInstance()->registerPlugin(new My_Layout_Plugin());

Related

Zend Framework translate language text

I have some translation code which is working fine.
<?php echo $this->translate("54"); ?>
Outputs
Hello World
is it possible to output instead of above
<div class='lang' id='54'>Hello World</div>
Later using jquery I would like to manipulate the div.
create a custom view helper named MyTranslate extends \Zend\I18n\View\Helper\Translate then override the _invoke method :
public function __invoke($message, $textDomain = null, $locale = null)
{
$t = parent::__invoke($message, $textDomain , $locale);
//change the value of $t however you wnat
return $t;
}
if you don't what to change your code where ever you have used translate register this new view helper as translate and not my_translate
If you use Zend1, you can use a view helper like this:
Create a Helper directory in your library
for example, in my case I have this directory:
library/DoyDoy/Helper/
Create your helper like this:
library/DoyDoy/Helper/TranslateID.php
<?php
class DoyDoy_Helper_TranslateID extends Zend_View_Helper_Abstract
{
public function translateID($id)
{
return '<div class=\'lang\' id=\'' . $id . '\'>'. $this->view->translate($id) . '</div>';
}
}
Add your Helper in the bootstrap:
protected function _initDoyDoyView(){
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('DoyDoy/Helper/', 'DoyDoy_Helper');
}
In your view, call the helper like this:
<?php echo $this->translateID("54");?>
This should display:
<div class='lang' id='54'>Hello World</div>
I hope it will help you :)

Hide and show navigator menu items, buttons and anchors using ACL

I am using ACL to grant resources to roles in the system, the allowed actions is excuted and denied actions are routed to custom page, I want to show and hide menu elements at run time using resources at ACL, and also I want to show and hide anchors, buttons in views.
I make a helper class
class Zend_View_Helper_Permission extends Zend_View_Helper_Abstract
{
private $_acl;
public function hasAccess($role, $action, $controller)
{
if (!$this->_acl) {
$this->_acl = Zend_Registry::get("Acl");
}
return $this->_acl->isAllowed($role, $controller, $action);
}
}
I define the view helper in config.ini file like this
resources.view.helperPath.Zend_View_Helper = APPLICATION_PATH "/modules/privileges/views/helpers"
how can I use this helper to make views created at run time?
Your method name should match class name hence it should be permission instead of hasAccess.
I myself use a global method show() instead of using view helper
function show($action = null)
{
$request = Zend_Controller_Front::getInstance()->getRequest();
$action = $action === null ? $request->getActionName() : $action;
$module = $request->getModuleName();
$controller = $request->getControllerName();
if(!Zend_Registry::isRegistered('acl')) throw new Exception('Show function can only be called inside view after preDispatch');
$acl = Zend_Registry::get('acl');
$resource = $module . '#' . $controller;
return $acl->isAllowed(Zend_Auth::getInstance()->getIdentity(),$resource,$action);
}
To keep it simple it takes controller , module name from request object .
To hide edit action link in list action view simply doo
list.phtml code as follow
<h2>Listing page Only superadmin can see edit link</h2>
<?php if(show('edit')): ?>
Edit
<?php endif;?>
Update
The global function show was defined inside library/Util.php which was loaded inside
public/index.php
require_once 'Zend/Application.php';
require_once 'Util.php';

Start execution from indexAction() in IndexController in ZEND Framework

In indexAction() method of IndexController I have called the method of model which returns the list of employees(employeeList) from database.And then add this this employeeList to $view and then call $view->render('index.phtml') and index .phtml shows the employeeList.The code is ad follows:
IndexController.php
<?php
require_once('../Zend/Controller/Action.php');
require_once('../models/HRModel.php');
require_once('../Zend/View.php');
class IndexController extends Zend_Controller_Action
{
protected $hrModel;
public function init()
{
$this->hrModel = new Application_Model_HRModel();
}
public function indexAction()
{
$view = new Zend_View(array('scriptPath' =>'../views'));
$view->employeeList = $this->hrModel->queryAllEmployees();
echo $view->render('index.phtml');
}
}
Application_model_HRModel.php
<?php
require_once('Zend/Db.php');
require_once('Zend/Config/Ini.php');
class Application_Model_HRModel
{
protected $db=null;
public function queryAllEmployees() {
return $this->db->fetchAssoc("select comment from guestbook");
}
}
index.phtml
foreach ($this->employeeList as $emp):
extract($emp);
echo '$EMPLOYEE_ID';
echo $comment;
endforeach
Now I want to start the execution from indexAction() method.But how to do this?What should be the url to be entered in browser?In request parameter the controller will be IndexController and action will be indexAction.So Kindly help me in resolving this issue.
To execute this action, you need to call the Index Controller with index Action. So you use www.foo.bar/index/index or simple www.foo.bar.
If this dont work, you have maybe a error in your confoguration.
Or did i mistake your question?
How did you configure your PHP Error handling? Maybe you have a PHP error that is not displayed.

Error in view file

I'm new to zend framework.I'm getting the following error when trying to view the page
Fatal error: Using $this when not in object context in D:\xampp\htdocs\neemjobs\application\views\scripts\register\index.phtml on line 1
class RegisterController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->pageTitle = "Zend_Form Example";
$this->view->bodyCopy = "<p >Please fill out this form.</p>";
$form = new forms_ContactForm();
$this->view->form = $form;
}
}
My View is
<?php echo $this->pageTitle ;?>
<?php echo $this->bodyCopy ;?>
This is totally correct (on my Zend Framework installation goes perfectly, I just don't understand "in which way" are you using the Form... but, anyway, it works, so the "bug" is not there...

Zend Framework: Need advice on how to implement a controller helper

i need advice on how i can implement this action helper. currently, i have something like
class Application_Controller_Action_Helper_AppendParamsToUrl extends Zend_Controller_Action_Helper_Abstract {
function appendParamsToUrl($params = array()) {
$router = Zend_Controller_Front::getInstance()->getRouter();
$url = $router->assemble($params);
if (!empty($_SERVER['QUERY_STRING'])) {
$url .= $_SERVER['QUERY_STRING'];
}
return $url;
}
}
but as you can see, i think the function should be a static function? but how will that find into this Zend_Controller_Action_Helper thingy?
Make the function public and in your BootStrap.php ensure the controller helper can be autoloaded
// add paths to controller helpers
Zend_Controller_Action_HelperBroker::addPath( APPLICATION_PATH .'/controllers/helpers');
You should then be able to call the helper from your controller via
$this->_helper->appendParamsToUrl->appendParamsToUrl();
You can also rename appendParamsToUrl() function to direct()
function direct( $params = array() ) {...}
In this case, you'll be able to access it from controller with
$this->_helper->appendParamsToUrl( $params );