当我使用我附加到类Controller的函数时,它在Yii中不起作用

时间:2013-12-26 06:24:04

标签: yii

我将一个xxx函数附加到类Controller,然后我触摸了一个名为' VideoController'的文件。它扩展了Controller。

当我执行VideoController时,无法调用xxx函数,为什么?

函数ajaxReturn:

class Controller extends CController
{
    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout='//layouts/column1';
    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu=array();
    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs=array();

    /**
     * zhoumengkang
     * 从Thinkphp里拖过来的
     */
    protected function ajaxReturn($data,$info='',$status=1,$type='JSON') {
        $result  =  array();
        $result['status']  =  $status;
        $result['info'] =  $info;
        $result['data'] = $data;
        if(strtoupper($type)=='JSON') {
            header("Content-Type:text/html; charset=utf-8");
            exit(json_encode($result));
        }elseif(strtoupper($type)=='XML'){
            header("Content-Type:text/xml; charset=utf-8");
            exit(xml_encode($result));
        }elseif(strtoupper($type)=='EVAL'){
            header("Content-Type:text/html; charset=utf-8");
            exit($data);
        }else{
            // TODO
        }
    }
}

但不能通过

调用
class VideoController extends Controller {
    public function actionTest() {
            $this->ajaxReturn(true,'test',1);
    }
}

1 个答案:

答案 0 :(得分:1)

您必须在扩展前导入控制器。

    Yii::import('application.controllers.Controller');
    class VideoController extends Controller {
    public function actionTest() {
            $this->ajaxReturn(true,'test',1);
    }
}

最好将您的控制器名称从Controller更改为someothername