Undefined Class Yii?

Hi guys!

I am new to the Yii Framework, and setup was a pain.

Now I’m trying to do a SQL query, which puts a PhP variable in a MYSQL database.

But I get the error Undefined Class Yii in <path>


use Yii;

$sql = “INSERT INTO accounts (username, password) VALUES (’$usrname’, ‘$passw’)”;

$command = Yii::$app->db->createCommand($sql);


Try using \Yii.

use \Yii or $command = \Yii… still gets me the same error

You are saying that "setup was a pain" … so I am curious: how exactly did you setup Yii ?

It is normally not a pain at all.

Does anything work?

How did you install?

Well ok, it wasn’t really that much of a pain. But I am so unfimiliar with Linux that caused it to take so long.

I installed it via Composer, and it constantly threw errors because I tried to create a project with the exact same name - which I later found out -

But then I had to install PhPMyAdmin, which I couldnt get all the way through the install, because it threw error 2002: HY00 Can’t connect to local MySQL server through socket. But when I ignored the error, it worked just fine.

Maybe it has something to do with this?

Doesn’t sound like it’s connected.

Where do you put your code from the first post? In a controller action?

I made an ‘Make new Account’ page in backend/web. That is where I put my code.

I could give you the whole file when I get home if you want to, but it’s not that interesting.

You’ll have to use the start script backend/web/index.php. Add a create action that will render a view to the backend/controllers/SiteController (or some new controller you create). Add a view to backend/views/<controllername>/<the view to render>

The Gii tool will simplify the task.

The Gii installation explanation is very unclear. I dont have a \config\web.php.

Note that I’m using the Advanced template.

If you are at the server, you should be able to use Gii directly.

From a remote browser, you’ll have to add the network or host to backend/config/main-local.php


    $config['bootstrap'][] = 'gii';

    $config['modules']['gii'] = [

        'class' => 'yii\gii\Module',

        'allowedIPs' => ['', '::1', '192.168.4.*'],


isn’t this tutorial for the basic template? when I go to localhost:8000/backend/web/index.php?r=gii I get my page that I created myself, which was completely white at first.

Yes, for the basic template (which is recommended unless two separate sites really are needed).

I don’t know about the internal Yii server (localhost:8000) but I just tried to start Gii using <myhost>/backend/web/index.php?r=gii with no problem.

for this assingment I need to use the advanced template.

also, what do you mean with <myhost>? Apache or something?

And I still get the exact same error, even with Gii…

Yes, Apache on a "remote" host.

Do you have the debug bar in the bottom of the page, possibly minimized right side?

If so click on the debug bar and check if you can find some clue from the log.

The only thing I have for debugging is the Terminal.

It says: Failed to open required <path> autoload.php in <path> index.php.

EDIT: I ran "composer install" in Terminal in the project folder and I somehow got the index.php UI to show…