2011-09-06 61 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