How to call Action Helper as broker method in View Helper? - zend-framework

I have Action Helper that is database abstraction layer.
I would like to access it in View Helper to read and present some data from the model.
In Controller I call Action Helper as broker method, but how to achieve the same in View Helper?
Somewhere in controller:
$this->_helper->Service("Custom\\PageService");
Service.php:
...
public function direct($serviceClass)
{
return new $serviceClass($this->em);
}

Nicer way will be to create a view helper inside it do
Zend_Controller_Action_HelperBroker::getStaticHelper('service')->direct("Custom\\PageService");
another way would be inside controller init method do
$this->view->helper = $this->_helper;
so in view (phtml) you can do
$this->helper->Service("Custom\\PageService");

Related

How to set ID of a XML-View in a component environment?

I want to access a view's controller from a custom module with some utility functions. Basically you can do this that way:
var oController = sap.ui.getCore().byId("__xmlview1").getController();
The problem is that the above coding will not work in a real environment because __xmlview1is dynamically created by the framework. So I tried to find a possibility to set the ID of the view during instantiation. The problem is - I could not find one:
Trying to give the view an ID in the view.xml file does not work:
<mvc:View
controllerName="dividendgrowthtools.view.dividendcompare"
id="testID"
xmlns="sap.m"
...
Trying to set the ID in the router configuration of the component does not work either:
...
name: "Dividend Compare",
viewId: "test",
pattern: "Dividend-Compare",
target: "dividendcompare"
...
The problem is that I do not have direct control over the instantiation of the XML view - the component respectively the router does it.
So, is there a solution for that problem? Or at least a save way to get the view ID by providing the name of the view?
You should have a look at the SAPUI5 EventBus.
I am pretty sure, you want to let the controller to do something with the dividentcompare view. With the SAPUI5 Eventbus, you can publish actions from one controller to another witout braking MVC patterns.
In your dividendcompare.controller.js:
onInit : function() {
var oEventBus = sap.ui.getCore().getEventBus();
oEventBus.subscribe("MyChannel", "doStuff", this.handleDoStuff, this);
[...]
},
handleDoStuff : function (oEvent) {
var oView = this.getView();
[...]
}
Now, in your anothercontroller.controller.js:
onTriggerDividendStuff : function (oEvent){
var oEventBus = sap.ui.getCore().getEventBus();
oEventBus.publish("MyChannel", "doStuff", { [optional Params] });
}
You are now able to get the view from the dividentcontroller in every case from every other controller of your app. You dont access the view directly, this would brake MVC patterns, but can pass options to its controller and do the handling there.

How to render methodAction of another Controller inside an action

I'm using extbase, fluid system on typo3 to build a backend module.
I Have a Controller "MainController" action called 'AddBoxes' and I have another Controller called BoxElementsController, and there is an action method called 'popupBoxAction'.
I want to render the output of the BoxElementsController->popupBoxAction in the MainController-AddBoxesAction();
so that I can assign the output to my view variable.
How can i achieve this in Typo3 6.1.
Thanks
Also you can fetch data or output whatever you like from BoxElementsRepository
class MainController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
protected $boxElementsRepository;
public function injectBoxElementsRepository(BoxElementsRepository $boxElementsRepository) {
$this->boxElementsRepository = $boxElementsRepository;
}
public function AddBoxesAction(){
$popupBoxActionOutput = $this->boxElementsRepository->popupBox();
$addBoxesAction = $this->mainRepository->findAll();
$this->view->assignMultiple(array(
'popupBoxActionOutput' => $popupBoxActionOutput,
'addBoxesAction' => $addBoxesAction,
));
}
}
Try to instantiate you controller in you action then call ControllerObject->initializeAction() before calling your desired action.

How to disable view renderer from a controller action helper ?

In a controller, I can call viewRenderer helper like this :
$this->_helper->viewRenderer->setNoRender(true);
How can I call viewRenderer in a controller action helper?
Assume that I have a Controller action helper :
class Zend_Controller_Action_Helper_Ajaxrequest extends Zend_Controller_Action_Helper_Abstract{
public function test(){
//what I should do here
}
}
viewRenderer in your example is actually an action helper, not a view helper.
To call action helpers, use the helper broker:
$helper = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$helper->setNoRender(true);
If you actually want to call view helpers, you need a view instance. You can get one from the controller:
$controller = $this->getActionController();
//call the url view helper
$controller->view->url(...);

Whats the way to use Zend_Acl in View to show/hide parts of view

I am wondering whats the way to use Zend_Acl to show/hide parts of view? I am thinking I will
Create a Controller Plugin that passes the logged in user + acl to view
$this->view->loggedInUser = Zend_Auth::getIdentity();
$this->view->acl = Zend_Registry::get('acl');
Then in view scripts do something like
$this->acl->isAllowed($this->view->loggedInUser, 'resource', 'privilege');
Or is there a better way? Or should I use a View Helper? That returns a boolean whether the logged in user is allowed?
You are using it in the view, so for me ViewHelper is correct place for that - I've done it once that way:
class Zend_View_Helper_HasAccess extends Zend_View_Helper_Abstract
{
private $_acl;
public function hasAccess($role, $controller, $action)
{
if (!$this->_acl) {
$this->_acl = Zend_Controller_Front::getInstance()->getPlugin('Acl');
//In yout case registry, but front controller plugin is better way to implement ACL
}
return $this->_acl->isAllowed($role, $controller, $action);
}
}

How do i assign a custom helper to each view differently in zend framework

i have a custom helper written which returns the html form as string which extends the Zend_view-hepler_Abstract
Now i have 3 helpers .How do i assign each helper to a different view .
It is something like this in the controller
class abc extends Zend_controller_front{
public action page1Action (){
// I want to use a different Helper
//How do i assign custom1 helper to this view Separately
}
public action page2Action (){
// I want to use a different Helper
//How do i assign custom2 helper to this view Separately
}
public action page3Action (){
// I want to use a different Helper
//How do i assign custom3 helper to this view Separately
}
}
um, should you be inheriting from Zend_Controller_Action not Zend_Controller_Front?
also the word 'action' is not a valid php keyword?
just use the view helper in your view script, so in abc/page1.phtml
print $this->page1Helper()
similarly for page2 and page3
but there may be an easier way... you can just
print $this->form;
and the form will print without the need for a helper?