dpgjci27392 2014-01-07 10:34
浏览 27
已采纳

ZF2如何调用函数

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; ?>
  • 写回答

2条回答 默认 最新

  • doubi4617 2014-01-07 10:50
    关注

    What you're looking at is a ViewHelper.

    Please also see this simple tutorial for a very instruction on how to create yourself one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图