ACL check in the view - php

Well in ZF1 there was the isAllowed view helper to check the ACL in the view.
But I can't find an equivalent of it for ZF2. The closest I got was the ZF2 navigation view helper, but the problem with that is that it requires a AbstractPage. I just want to throw a resource/privilege to it, example:
$this->allowed('resource', 'privilege');
Is there not such a view helper, or am I looking at this all the wrong way?

Well I couldn't find any view helper, neither got any response so I wrote my own view helper.
isAllowed view helper code

Related

How to call viewHelper via OPT

I work with Zend Framework 1.12 and Open Power Template.
In ZF I made view helper InfolineData with method getDispayInfoline($lang) (class name Callcenter_View_Helper_InfolineData) and I pass this helper via controller to view.
Controller Code:
$this->view->openHours = $this->view->getHelper('infolineData')->getDispayInfoline($lang);
View Code:
{$openHours}
How can I make similar call using only Open Power Template?
Ok, I find solution
$this->view->getHelper('infolineData')->getDispayInfoline($lang)
is equal to
{u:$view::getHelper('infolineData')::getDispayInfoline($view::language)}

Laravel : Using controller in included view

I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.
How do I use my controller when I include my view?
I'm on Laravel 3.
Right now I have my controller :
public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}
My route (which obv only work on GET /current) :
Route::get('current', 'sale#current');
My master view
#include('sale.current')
Then my sale.current view calls $sales
#foreach($sales as $sale)
Thanks!
So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.
When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.
Here is the official documentation link : https://laravel.com/docs/master/views#view-composers
Here is the more detailed version of it :
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers
This is the standard way of achieving it without any patch work.
Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:
Route::get('test', 'TestController#test');
In TestController I define the action test as follows:
public function test()
{
return View::make('test.home')->with('data', array('hello', 'world', '!'));
}
According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:
// home.blade.php
<h1>Message</h1>
#include('test.test')
and
// test.blade.php
<?php print_r($data); ?>
When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.
I hope that can help you.

Use view helpers in controllers in Zend Framework

I have a controller that is called with AJAX (sends JSON data), so I don't use a view.
I need to use a personnal view helper to format my data, but in my controller.
Is that possible ?
Or maybe I am doing it wrong (maybe I should have a view, but how with JSON) ?
You can access any ViewHelper from the Controller by
$this->view->helpername(/*params*/);
// or
$helper = $this->view->getHelper('helpername');
// or
$broker = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$broker->getView()->helpername(/*params*/);
See Zend: How to use a custom function from a view helper in the controller?
However, you might be right that you are doing it wrong (funny pic btw), but I cannot really tell from your question. Please refine it as to why you need to call the view helper and what it is supposed to format.
Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
Just be sure that the returned view is the view you want. Because down the line, the view may get overwritten and on the controller you have a spank new view.
And all those values you setup on the view on the action helper and the like... before the controller is kicked in? All gone with the wind!
So test before assuming that if you get a view resource. it is really the same view resource you expect, and that all your vars are still there.
You may be surprised as i was!
You can create an instance of a Helper .. this will work in Controllers, Models and everywhere you need the Helper.
eg.
// create Instance
$serverUrl_helper = new Zend_View_Helper_ServerUrl();
// get the ServerUrl
$serverUrl = $serverUrl_helper->serverUrl();
Another approach is to use the ContextSwitch or AjaxContext action-helpers. This allows you to use a view-script from which you can then call your view-helper in the standard manner.
Just use action helpers, many of view helpers are available as action helpers too.
Or directly by using Zend_Date or sprintf.

Method/function overrides in Codeigniter

I want to override the validation_errors() method of the form helper in CodeIgniter for one controller only, so that it will display a single error message (as a sentence in plain english) instead of the detailed line item summary. I've tried defining a validation_errors() function in my controller, which is what I usually do with Silverstripe's Sapphire framework, but this won't with CI.
What's the best way to override methods for a case by case basis?
You can create a MY_Form_helper.php in your application/helpers/ folder and create that function in there and then when you load the form helper, it should also load your function.
For more information read the '"Extending" Helpers' section on: http://codeigniter.com/user_guide/general/helpers.html

Symfony: Is it possible to setTemplate for components?

There’s no setTemplate() for components! I know but maybe there is another way to do it ?
(The question seems to be about a php framework: http://www.symfony-project.org/)
There is no setTemplate method on sfComponents. You essentially have 3 options:
Name your component the same as the partial you'd like the component to render. This may not be possible if you have multiple components you'd like to share the same template.
Create a partial with the same name of your component and include the partial there. That is, if you had a component with an executeFoo() method that you wanted to render the _bar.php template, simply call include_partial('bar', $vars) inside of _foo.php.
Load the PartialHelper and render the partial manually inside of the components execute method and have the component return sfView::NONE.
Components don't handle templates, you can only use partials. If you need to return a specific partial from inside your components class you can do something like this:
return get_partial('module/action', array('paramName' => $paramValue));
Have a look into the symfony book, chapter 7 view layer
To get around this, i'm doing:
echo get_component('module', 'action', $this->getVarHolder()->getAll());
return sfView::NONE;
This worked for me:
$this->setVar('template', 'templateName');
Obviously the template have to be in the exactly same module.

Categories