flash messenger不显示消息

时间:2015-01-22 07:28:19

标签: php zend-framework flash-message

我刚刚开始使用Zend Framework开发一个网站。 网站上的Flash消息已被破坏,我的工作是修复它。 我试图搞清楚它有什么问题,并且在理解代码方面取得了部分成功。

之前我从未在Zend Framework中工作过,因此我不熟悉可以解决的问题,甚至不知道如何正确调试它。

我试过放置this question中建议的代码,但它完全打破了php。

如果有人可以帮我解决这个问题,我真的很感激: 我将在下面包含文件:


LoginController.php,它扩展了MrBlue_Controller_Action_Default 并以下列方式调用flash msg:

{
                    $this->_flashMessenger->addMessage(array('success', 'Done. Now get your email and confirm you account'));
                    $this->redirect('/');
}

MrBlue_Controller_Action_Default扩展了Zend_Controller_Action

<?php

/**
 * Default.php
 */
abstract class MrBlue_Controller_Action_Default extends Zend_Controller_Action
{

    /**
     * Zend_Acl
     *
     * @var Zend_Acl
     * @access protected
     */
    protected $_acl = null;

    /**
     * FlashMessenger
     *
     * @var object
     * @access protected
     */
    protected $_flashMessenger = null;

    /**
     * Array with params (controller, action, etc)
     *
     * @var array
     * @access protected
     */
    protected $params;

    /**
     * Application config object
     *
     * @var Zend_Config
     * @access protected
     */
    protected $config;

    /**
     * MrBlue Benchamark Helper
     *
     * @var MrBlue_Helper_Benchmark
     * @access protected
     */
    protected $benchmark;

    /**
     * Zend_Auth
     *
     * @var Zend_Auth
     * @access protected
     */
    protected $_auth = null;

    /**
     * User account
     *
     * @var object
     * @access protected
     */
    protected $account = null;

    /**
     * Array of post values
     * @var array
     */
    protected $aPost = array();

    /**
     * @var Zend_Session_Namespace
     */
    protected $oDefaultUserNS = null;

    /**
     *  init()
     *
     * @access public
     */
    public function init()
    {
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->messages = $this->_flashMessenger->getMessages();

        $this->params = $this->getAllParams();
        $this->params['c/a'] = $this->params['controller'] . '/' . $this->params['action'];
        $this->view->params = $this->params;

        if ($this->_request->isPost())
        {
            $this->aPost = $this->_request->getPost();
        }
        $this->view->aPost = $this->aPost;

        $this->config = Zend_Registry::get('config');
        $this->view->config = $this->config;

        // Zend ACL
        $this->_acl = Zend_Registry::get('acl');
        $this->view->acl = $this->_acl;
        $this->view->role = 'guest';    // will be overwrite in setAccountData()


        //create navigation container
        $this->prepareNavigation();

        $this->oDefaultUserNS = new Zend_Session_Namespace('default_data');

        $this->benchmark = new MrBlue_Helper_Benchmark();
        //$this->view->benchmark = $this->benchmark;

        $this->setAuth();
    }

    /**
     * Prepare navigation container based on navigation.php file and
     * dynamic pages from database 
     */
    public function prepareNavigation()
    {
        //get current role or if null default guest role with limited access
        $role = 'guest';
        if( Zend_Auth::getInstance()->hasIdentity() ) {
            $role = Zend_Auth::getInstance()->getIdentity()->role;

            /*
             OLD CODE to display leaderboard menu based on leader_board flag in users table
             * 
             *
            //allow Leaderboard page if user have "List me on leader board" flag set to 1
            //flag has to be check from database  - not from identity, because it can change in session
            $user_app = new Model_UsersApp();
            $user = $user_app->getById(Zend_Auth::getInstance()->getIdentity()->id);

            if($user->leader_board == 1)
            {
                $this->_acl->allow('user', 'default:leaderboard', null);
            }
            else
            {
                $this->_acl->deny('user', 'default:leaderboard', null);
            }
             *
             */
        }

        /* 
         * NEW CODE - to display leaderboard menu item based on flag in settings table
         */
        //allow Leaderboard page 
      //  $settings_app = new Model_SettingsApp();

        if(MrBlue_Setting::get('leaderboard_menu')==1)
        {
             $this->_acl->allow('user', 'default:leaderboard', null);
        }
        else
        {
            $this->_acl->deny('user', 'default:leaderboard', null);
        }



        //create navigation from navi file
        $navConfig = require APPLICATION_PATH . '/modules/default/configs/navigation.php';
        $navigation = new Zend_Navigation($navConfig);

        //get dynamic pages
        $pages_app = new Model_PagesApp();
        $pages = $pages_app->getList(1);

        //add dynamic pages to navigation
        foreach ($pages as $page)
        {
            if( $page['roles']==Model_Db_PagesDb::ROLE_ALL || $page['roles']==$role ) {
                $page_nav = new Zend_Navigation_Page_Mvc(array(
                    'route' => 'pages',
                    'module' => 'default',
                    'controller' => 'pages',
                    'action' => 'read',
                    'params' => array('pageslug' => $page['name_slug']),
                    'label' => $page['name'],
                    'position' => $page['position'],
                    'roles' => $page['roles']
                ));

                $navigation->addPage($page_nav);
            }
        }

        //send navigation to view container
        $this->view->navigation($navigation)->setAcl($this->_acl)->setRole($role);
    }

    /**
     * Set auth
     *
     * @access public
     * @return void
     */
    public function setAuth()
    {
        $this->_auth = Zend_Auth::getInstance();
        $this->_auth->setStorage(new Zend_Auth_Storage_Session('Default_Auth'));

        if ($this->_auth->hasIdentity())
        {
            $this->setAccountData();
        }
        else
        {
            if ($this->params['c/a'] != 'login/authorize')
            {
                //@TASK uncomment redirection for denied resources
                // return $this->redirect('index/index');
            }
        }
    }

    /**
     * Checking if some action in current module and controller is allowed
     *
     * @param string $action
     * @return boolean
     */
    public function isActionAllowed($action)
    {
        return $this->_acl->isAllowed($this->account->role, $this->_request->getModuleName() . ':' . $this->_request->getControllerName(), $action);
    }

    /**
     * Set account data
     *
     * @access public
     * @return void
     */
    public function setAccountData()
    {
        if ($this->_auth->hasIdentity())
        {
            $this->account = $this->_auth->getIdentity();

            $climbing_app = new Model_ClimbingApp();
            $climbing = $climbing_app->getUserLastClimbing($this->account->id);
            $this->account->climbing = $climbing;
//            $this->account->funds = $climbing['funds'];
//            $this->account->coins = $climbing['coins'];
            unset($climbing_app);

             //must be ass separate query because if user have no climbing records, there is nothing to join user table values
            $user_app = new Model_UsersApp();
            $user = $user_app->getById($this->account->id);

            $this->account->funds = $user->funds;
            $this->account->coins = $user->coins;

            unset($user_app);

            $this->view->account = $this->account;
            $this->view->role = $this->account->role;

            // Zend_Registry
            Zend_Registry::set('account', $this->account);
            Zend_Registry::set('auth', $this->_auth);
        }
    }

    /**
     * Get current climbing values
     * 
     * @return array
     */
    public function getUserClimbing()
    {
        $aClimbing = array();

        if ( $this->_auth->hasIdentity() && isset($this->account->climbing) && !empty($this->account->climbing) )
        {
            $aClimbing = $this->account->climbing;
        }

        return $aClimbing;
    }

}

index.php我想显示信息:

<section class="landing-main landing-main2">
            <div class="landing-logo">
               <!-- <h1>
                    Sports Ladder <br />
                    <span>Challenge</span>
                </h1>
                <div class="logo-star">
                    &#9733;
                </div> -->
                <img src="http://www.sportsladderchallenge.com/img/Logo.png" alt="Sports Ladder Challenge" />
            </div>
            <section class="main-highlight">

                 <div class="join-section-right">
                    <p class="cta-caption">
                        Win up to <span>$50,000.00</span>
                     </p>            
                     <a class="cta" href="<?php echo $this->url(array('controller'=>'login','action'=>'register'),null,true); ?>" alt="" id="signup-button" />
                        Join Now <span>&raquo;</span>
                     </a>
                     <p class="cta-caption">
                        It's absolutely 100% <strong>free</strong>. 
                     </p>

                 </div>
            </section>

    <section class="sports-ladder">
                <?php if($this->double == 2) $d = true; else $d = false;  
            echo $this->partial('partials/ladder_sidebar.phtml',array('ladder' => $this->ladder,'safe_zones' => $this->ladder_safe, 
            'level' => $this->current_rung, 'double' => $d) );
    ?>

    <br />
    <?php
    $oLadder = new Model_LadderObj();
    if( !is_null($this->current_rung) && !in_array($this->current_rung,$oLadder->getSafeZones()) && $this->current_rung!=0 ) :
    ?>
        <a class="header-buttons ladder-collect" href="<?php echo $this->url(array('action'=>'respawn-climbing'),null,true); ?>" class="btn btn-primary btn-collect" style="margin-left:16px; width:207px;" title="By pressing ‘collect’ you will have the specified dollar amount on any non-safe step (steps 5,6,8,9 etc) deposited into your account balance and forfeit the remainder of your climb. This action cannot be undone.">Collect</a>
    <?php else : ?>
        <a class="header-buttons ladder-collect" href="javascript:return false;" class="btn btn-default btn-collect" style="margin-left:16px; width:207px;" title="By pressing ‘collect’ you will have the specified dollar amount on any non-safe step (steps 5,6,8,9 etc) deposited into your account balance and forfeit the remainder of your climb. This action cannot be undone.">Collect</a>
    <?php endif; ?>
            </section>

            <section class="main-htp">
                <h2>
                    How To Play
                </h2>
                <h4>
                    And win up to <span>$50,000.00</span>!
                 </h4>
                <div class="htp">
                    <div class="htp-number">
                        1
                    </div>
                    <h3>Sign Up</h3>

                    <li>
                        Sign up, it's completely free to play!
                    </li>
                    <li>
                        Pick one of our sports match ups
                    </li>


                </div>
                <div class="htp">
                    <div class="htp-number">
                        2
                    </div>
                    <h3>Build Your Streak</h3>
                    <li>
                        Make a right pick, and climb a step
                    </li>
                    <li>
                        The higher you climb, the more money you win!
                    </li>

                </div>
                <div class="htp">
                    <div class="htp-number">
                        3
                    </div>
                    <h3>Cash Out</h3>
                    <li>
                        Earn anywhere from $0.01 to $50,000.00 per climb
                    </li>
                    <li>
                        Increase your account balance to $100 or more and cash out, it's that easy!
                    </li>

                </div>
            </section>

            <section class="main-bottom-left">
                <h2>Start Playing Today</h2>
                <div class="join-section-left">
                    <div class="join-section">
                        <p class="star">&#10097;</p>
                        <p>
                            The Sports Ladder Challenge combines the use of power ups, a wider selection of games and questions to choose from, as well as an innovative new platform that is optimized to reward users on a variety of levels to combine for the most thrilling, action packed pick em game on the web!
                        </p>

                    </div>

                    <div class="join-section">
                        <p class="star">&#10097;</p>
                         <p>
                            We cover a wide variety of sports and leagues including the NFL, NBA, MLB, NHL, Premier League, La Liga and much more! 
                        </p>

                    </div>
                    <div class="join-section">
                        <p class="star">&#10097;</p>
                        <p>
                            There is no limit to how much you can win! Win anywhere from $0.01 to $50,000.00 on every climb. We GUARANTEE that with enough sports knowledge and due diligence, you will cash out!
                         </p>

                     </div>
                 </div>

            </section>
        </section>

_flash_messenger.phtml partial

<?php if ( !empty($this->messages) ): ?>
    <div style="margin-left:auto;margin-right:auto;width:90%;">
    <?php foreach($this->messages as $message): ?>
        <?php if($message[0] == 'error'): ?>
        <div class="alert alert-block alert-danger">
        <?php elseif($message[0] == 'success'): ?>
        <div class="alert alert-block alert-success">
        <?php endif; ?>
            <p><strong><?php echo $message[1]; ?></strong></p>
        </div>
    <?php endforeach; ?>
    </div>
<?php endif; ?>

我非常感谢能得到的任何帮助。 如果此信息不完整,请告诉我,我将发布其他代码段。

更新 LoginController.php的完整代码 链接:http://codepad.org/VL4DGLtX

1 个答案:

答案 0 :(得分:1)

看起来消息没有呈现。

如果你看一下你提供的抽象控制器,就会有这样的代码:

$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
$this->view->messages = $this->_flashMessenger->getMessages();

这会抓取flash messenger动作帮助器(doc)并将所有消息放入视图(模板)。

现在,您需要将部分模板_flash_messenger.phtml渲染到您想要这些消息的位置。为此,您可以使用部分视图助手(doc):

<?php echo $this->partial('path/to/_flash_messenger.phtml', array('messages' => $this->messages)); ?>