1

Hello I need to use evalmath.class.php within a Yii application. How can I include it from my controller?

Something like this:

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

But the above code does not work. How can I include an external class within a controller?

2 Answers 2

2

In your example, to use include/require you probably need to add some path info with dirname(__FILE__).'/../models/...' or similar, but to do it within the Yii framework, you would first create an alias (usually in your main config file) with setPathOfAlias :

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

Then you can use Yii::import like so:

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

and proceed as you were:

$m = new EvalMath();
..etc...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It works. EvalMath is correctly included. But now, I got another problem. Inside Yii, operations with parentheses don't work, while outside they do. Even the simple $m->evaluate('(2+2)'); breaks the program.
Solved but I don't know why that happens. Now the result has nothing when pulling the formula from the database. You can have a look to my code if you want at github.com/ivantxo/Yii-MathEval/blob/master/matheval/protected/…. The function is actionFormulas, and you will need to create a database in mysql called matheval, there is a sql script at the root of the repository. matheval.sql. thanks
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.