2011-09-06 62 views
1

您好,我需要在Yii应用程序中使用evalmath.class.php。我如何从我的控制器中包含它?Yii的外部Php类?

事情是这样的:

public function actionFormulas() { 
    include('models/evalmath.class.php'); 
    $m = new EvalMath; 
    ... 
} 

但上面的代码不起作用。我如何在控制器中包含外部类?

回答

2

在你的例子中,要使用include/require,你可能需要添加一些dirname(__FILE__).'/../models/...'或类似的路径信息,但要在Yii框架内完成,你需要首先创建一个别名(通常在你的主配置文件中) setPathOfAlias

Yii::setPathOfAlias('evalmath', $evalmath_path); 

然后你可以使用Yii::import像这样:

Yii::import('evalmath', true); 

,并继续为你:

$m = new EvalMath(); 
..etc... 
+0

谢谢。有用。 EvalMath已正确包含在内。但现在,我又遇到了另一个问题。在Yii内部,带圆括号的操作不起作用,而在外部操作。即使是简单的$ m-> evaluate('(2 + 2)');打破了计划。 – ivantxo

+0

确切的错误是什么? – ldg

+0

解决了,但我不知道为什么会发生。从数据库中提取公式时,结果没有任何结果。你可以看看我的代码,如果你想在https://github.com/ivantxo/Yii-MathEval/blob/master/matheval/protected/controllers/CalcController.php。该函数是actionFormulas,你需要在mysql中创建一个名为matheval的数据库,在仓库的根目录下有一个sql脚本。 matheval.sql。谢谢 – ivantxo

0
class ServiceController extends Controller 
{ 
    public function actionIndex() 
    { 
     Yii::import('application.controllers.back.ConsolidateController'); // ConsolidateController is another controller in back controller folder 
     echo ConsolidateController::test(); // test is action in ConsolidateController