Yii PHP框架:无法在Yii中使用require_once

时间:2013-02-13 13:13:21

标签: php extjs yii

我正在尝试将重写的Ext.Direct功能实现到Yii中。一切顺利,但我无法使doRpc函数正常工作,这对于应用程序正常工作至关重要,该函数在Sencha Ext.Direct Tutorial中定义,尽管我已对其进行了一些更改,所以这是我的重写代码:

组件/ Ext_Router.php

class Ext_Router extends CApplicationComponent
{
    public function doRpc($cdata)
    {
        $API = Yii::app()->ext_api->getApi();
        try {
            if(!isset($API[$cdata->action])){
                throw new Exception('Call to undefined action: ' . $cdata->action);
            }

            $action = $cdata->action;
            $a = $API[$action];

            Yii::app()->ext_router->doAroundCalls($a['before'], $cdata);

            $method = $cdata->method;
            $mdef = $a['methods'][$method];
            if(!$mdef){
                throw new Exception("Call to undefined method: $method on action $action");
            }
            Yii::app()->ext_router->doAroundCalls($mdef['before'], $cdata);

            $r = array(
                'type'=>'rpc',
                'tid'=>$cdata->tid,
                'action'=>$action,
                'method'=>$method
            );

            // TODO require_once always returns: No such file or directory
            require_once("index.php?r=direct/classes/index&classaction=$action");

            $o = new $action();
            if (isset($mdef['len'])) {
                $params = isset($cdata->data) && is_array($cdata->data) ? $cdata->data : array();
            } else {
                $params = array($cdata->data);
            }

            $r['result'] = call_user_func_array(array($o, $method), $params);

            Yii::app()->ext_router->doAroundCalls($mdef['after'], $cdata, $r);
            Yii::app()->ext_router->doAroundCalls($a['after'], $cdata, $r);
        }
        catch(Exception $e){
            $r['type'] = 'exception';
            $r['message'] = $e->getMessage();
            $r['where'] = $e->getTraceAsString();
        }
        return $r;
    }

    public function doAroundCalls(&$fns, &$cdata, &$returnData=null)
    {
        if(!$fns){
            return;
        }
        if(is_array($fns)){
            foreach($fns as $f){
                $f($cdata, $returnData);
            }
        }else{
            $fns($cdata, $returnData);
        }
    }
}

模块/直接/控制器/ ClassesController.php

class ClassesController extends Controller
{
    public function actionIndex($classaction)
    {
        $this->render("$classaction");
    }
}

模块/直接/视图/类/ Temporary.php

class Temporary
{
    protected $_result;
    public $results;

    public function getResults(stdClass $params)
    {
        $_result = Temporary::model()->findAllByAttributes(array('id'=>Yii::app()->session['id']));

        $results = array();

        if($_results != null) {
            foreach($_result as $row) {
                $results[] = $row;
            }
        }

        return $results;
    }   

    ...
}

问题:

require_once("index.php?r=direct/classes/index&classaction=$action");

绕过这个问题的最佳方法是什么?我如何'解析'/ include / require这个PHP文件?

(请记住,我还没有去测试我的Temporary.php,因为我根本没有超过当前的错误)

1 个答案:

答案 0 :(得分:2)

使用Yii's import

<?php
Yii::import('application.modules.direct.views.classes.Temporary');