Zend Framework从另一个控制器访问方法的问题

时间:2012-08-22 17:15:47

标签: php zend-framework zend-controller

我一直在尝试使用_forward方法从另一个控制器访问我的IndexController中的方法,但它似乎不起作用。我试过了:

$this->_forward('permissions', 'Index')
$this->_forward('permissions', 'index')
$this->_forward('permissions', 'IndexController')
$this->_forward('permissions', 'indexcontroller')

这些都没有奏效。这是我的代码。

控制器     

class IndexController extends Zend_Controller_Action
{

    /**
     * facebookConfig
     * 
     * @var mixed
     * @access protected
     */
    protected $facebookConfig;

    /**
     * signed_request
     * 
     * @var mixed
     * @access protected
     */
    protected $signed_request;

    /**
     * facebookConfig
     * 
     * @var mixed
     * @access protected
     */
    protected $permissions;

    /**
     * init function.
     * 
     * @access public
     * @return void
     */
    public function init()
    {
        // get the model
        $this->app = new Application_Model_Admin();

        // get config data
        $this->facebookConfig =  $this->app->configData();

        // get the apps permissions
        $this->permissions = $this->permissions();

        // get the data for the head
        $data = $this->app->configData();
        if(empty($data->ogimage))
            $data->ogimage = '';

        // set the page title
        $this->view->headTitle($data->title, 'PREPEND');

        // set the meta data
        $this->view->headMeta()->setName('fb:app_id',       $data->appid);
        $this->view->headMeta()->setName('og:type',         $data->ogtype);
        $this->view->headMeta()->setName('og:title',        $data->ogtitle);
        $this->view->headMeta()->setName('og:description',  $data->ogdesc);
        $this->view->headMeta()->setName('og:url',          $data->applink);
        $this->view->headMeta()->setName('og:image',        $data->ogimage);
    }

    /**
     * permissions function.
     * 
     * @access public
     * @return void
     */
    public function permissions(){
        // return the permissions
        return 'publish_stream, read_stream, friends_likes';
    }

}

第二控制器

<?php

class ThanksController extends Zend_Controller_Action
{

    /**
     * facebookConfig
     * 
     * @var mixed
     * @access protected
     */
    protected $permissions;

    /**
     * init function.
     * 
     * @access public
     * @return void
     */
    public function init()
    {
        // get the model

        // get the permissions
        $this->permissions = $this->_forward('permissions', 'index');
        print_r('<pre>');var_dump($this->_forward('permissions', 'index'));print_r('</pre>');die;

    }
 }

3 个答案:

答案 0 :(得分:2)

_forward仅用于调用控制器操作,而不是常规方法

_forward('index')实际上是指方法indexAction()

答案 1 :(得分:1)

改变这个:

public function permissions(){
    // return the permissions
    return 'publish_stream, read_stream, friends_likes';
}

到此:

public function permissionsAction(){
    // return the permissions
    return 'publish_stream, read_stream, friends_likes';
}

答案 2 :(得分:0)

我没有特别清楚地思考,而是将代码放在模型中并从他们那里访问数据。