Reputation: 959
In my page I have a sidebar and I want to display the admin links just if the user has the admin rights. My method work fine in the controllers but when I use him in the view I have an error:
PHP Fatal error: Call to a member function get() on a non-object in /UNXDEVCKT01/www/firewall/ZendFramework/module/Firewall/src/Firewall/Service/UserRight.php on line 59
I don't understand that doesn't work...
My class UserRight class:
<?php
namespace Firewall\Service;
use Zend\Mvc\Controller\ActionController;
use Zend\Session\Container;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\TableGateway\Feature\RowGatewayFeature;
use Zend\Db\TableGateway\TableGateway;
use Zend\Mvc\Controller\Plugin\FlashMessenger;
use Zend\Http\Request;
class UserRight extends AbstractActionController
{
# Boolean
public function hasAdminRight()
{
# Call the User service table
$Rights = $this->getServiceLocator()->get('Firewall\Model\UserTable')->fetchAllList($this->session->user_id);
echo $Rights->right_admin. ' '. $Rights->user_group_admin;
# Check is the user have the admin rights, else he's redirected
return (($Rights->right_admin == 'n' && $Rights->user_group_admin == 'n')) ? false : true;
}
// Code ...
}
?>
My view :
<h3>Template</h3>
<ul class="toggle">
<li class="icn_folder"><a href="#">File manager</a></li>
<li class="icn_photo"><a href="#">Gallery</a></li>
<li class="icn_audio"><a href="#">Audio</a></li>
<li class="icn_video"><a href="#">Video</a></li>
</ul>
<br/>
<?php
$UserRight = new \Firewall\Service\UserRight;
if($UserRight->hasAdminRight()):
?>
<hr/><h3>Administration</h3>
<ul class="toggle">
<li class="icn_add_user"><a href="<?php echo $this->url('user', array('action'=>'add'));?>">New user</a></li>
<li class="icn_categories"><a href="<?php echo $this->url('user', array('action'=>'list'));?>">User list</a></li>
<br/>
<li class="icn_profile"><a href="<?php echo $this->url('profile', array('action'=>'add'));?>">New profile</a></li>
<li class="icn_categories"><a href="<?php echo $this->url('profile', array('action'=>'list'));?>">Profile list</a></li>
<br/>
<li class="icn_view_users"><a href="<?php echo $this->url('group', array('action'=>'add'));?>">New group</a></li>
<li class="icn_categories"><a href="<?php echo $this->url('group', array('action'=>'list'));?>">Group list</a></li>
<!--<br/>
<li class="icn_settings"><a href="#">Options</a></li>
<li class="icn_security"><a href="#">Security</a></li>
<li class="icn_jump_back"><a href="#">Logout</a></li>-->
</ul>
<?php endif; ?>
Upvotes: 0
Views: 366
Reputation: 10947
Let me explain something, so you understand the origin of the problem:
It is true that if you create a controller, extending AbstractActionController
, you will have a class with all the functions of the parent classes, and you can think that you can use them. And of course you can since the are defined. But the return values of that functions are not based in something magic, and if for example $this->getServiceLocator() returns the service locator, is not because the controllers has a magic ability to find the service locator: it can return it because somebody injected it before.
So, whats your problem? your problem is that you are creating your UserRight
by hand, and you are expecting it to have all the habilities that it has when the system creates a controller. But the system doesnt create it just like $UserRight = new \Firewall\Service\UserRight
, but by means of a more complex procedure, injecting dependencies for example.
So, you cannot create a controller by hand, and expect it to have the same behavior as when it is created by the system, because it wont be a real controller. And more over: you shouldnt create a controller by hand, since they are not meant to that.
In your case, $this->getServiceLocator()
is not returning an object, since that function has not a clue of what the service manager is.
What you need is to put all the logic you need in the real controller that is dispatched before rendering that view, and pass the values to the view, or if its absolutely view related, you can create a view helper, following the tutorial from evan coury (one of the ZF2 gurus) that Sam (other of the ZF2 gurus) recommends in its answer
Upvotes: 1
Reputation: 16455
What you're looking at is a ViewHelper.
Please also see this simple tutorial for a very instruction on how to create yourself one.
Upvotes: 2