MHogge
MHogge

Reputation: 5196

Yii2 class not found on Ajax call

I have a model class that refer to a table of my database:

<?php

namespace app\models;

use yii\db\ActiveRecord;

class Pricing extends ActiveRecord
{

}

I try to use a simple php function on a separate php file (called pricing.php and placed in ../web):

<?php

use app\models\Pricing;

$pricings = Pricing::find()->all();
echo $pricings[0]->weekPrice;

?>

This php file aim to be called on a javascript event with ajax:

function actualizePrice() {
    $.ajax({
        url:'pricing.php',
        success: function (response) {
            actualize()
        },
        error: function () {
            console.log('error');
        },
    });
    return false;
}

But I get a Uncaught Error: Class 'app\models\Pricing' not found, any idea what is happening here?

Upvotes: 0

Views: 173

Answers (1)

csminb
csminb

Reputation: 2382

you shoudn't be creating any new php files in files files in /web

you need to create an action somewhere inside one of your yii controllers (or a make a new one) and point your js to that action (something like /site/pricing or index.php?r=site%2Fpricing)

Upvotes: 3

Related Questions