-2

I'm trying to implement a rewritten Ext.Direct functionality into Yii. And everything works smoothly except that I cannot get the doRpc function to work properly, and this is vital to the application working properly, the function is defined in the Sencha Ext.Direct Tutorial, although I have done some changes to it, so here's my rewritten code:

components/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);
        }
    }
}

modules/direct/controllers/ClassesController.php

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

modules/direct/views/classes/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;
    }   

    ...
}

The problem:

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

What is the best way of circumventing this problem? How do I 'parse'/include/require this PHP file?

(Have in mind that I haven't gotten around to testing my Temporary.php, because I simply have not gotten past the current error)

GauteR
  • 360
  • 7
  • 28
  • 2
    I 'm not sure what you are trying to do here. What is the expected result? – Jon Feb 13 '13 at 13:19
  • possible duplicate of [PHP - include a php file and also send query parameters](http://stackoverflow.com/questions/1232097/php-include-a-php-file-and-also-send-query-parameters) or [php include problem with urls with options ?view=task&others file not found](http://stackoverflow.com/questions/3592251/php-include-problem-with-urls-with-options-view-taskothers-file-not-found?rq=1) – deceze Feb 13 '13 at 13:20
  • I'm trying to use `require_once` to include the file specified in the call. For this case it's named `Temporary` so it will become `require_once("index.php?r=direct/classes/index&classaction=Temporary");` - but for other actions it will be something else. But `require_once` is always resulting in: `require_once(index.php?r=direct/classes/index&classaction=Temporary): failed to open stream: No such file or directory`. – GauteR Feb 13 '13 at 13:23
  • Because there is no file called `index.php?r=direct/classes/index&classaction=Temporary`. That's not a valid file name. `include` **file names**, not URLs. – deceze Feb 13 '13 at 13:25
  • Then how do I 'parse'/include/require this PHP file? What is the best way of circumventing it by using Yii? Since I cannot use URLs for require_once, and cannot use file names for Yii. – GauteR Feb 13 '13 at 13:30
  • @rnngau: I know that you are trying to "include" the file because that's what `require_once` *does*. What I am trying to find out is *what you think "include the file" means*, because obviously your mental mode of what "include" means is in some way off. Do you want to **run the code** or do you want **its output**? – Jon Feb 13 '13 at 13:50
  • I wanted the _Temporary class_ to be available to the rest of the application, aka. **run the code**. With @Asgaroth's answer below, it works like I wanted it to. – GauteR Feb 13 '13 at 14:07

1 Answers1

2

Use Yii's import :

<?php
Yii::import('application.modules.direct.views.classes.Temporary');
Asgaroth
  • 4,274
  • 3
  • 20
  • 36