Fatal error: Using $this when not in object issue

Hello,

I am new to the Yii Framework and I am trying to get my site up and running. I am running a WAMP server. When I enter the path into my web browser, (in this case 127.0.0.1/advanced/frontend/views/site/index.php), I get the following error:

Fatal error: Using $this when not in object context in C:\wamp\www\advanced\frontend\views\site\index.php on line 5

I have not changed any of the code. I will include the code at the end of this post. Is there a bug in Yii or something?

From what I have researched, I need to go into my .htaccess file and make some adjustments to that file but I am not sure.

I don’t know how to does this. Would anyone know where to go to read up on this information or would anyone be willing to guide me through editing this document?

Thanks in advance,

Danny

P.S. I didn’t know you had to install my index.php file so I am going to paste the code in here:

<?php

/* @var $this yii\web\View */

$this->title = ‘My Yii Application’;

?>

<div class="site-index">

&lt;div class=&quot;jumbotron&quot;&gt;


    &lt;h1&gt;Congratulations&#33;&lt;/h1&gt;





    &lt;p class=&quot;lead&quot;&gt;You have successfully created your Yii-powered application.&lt;/p&gt;





    &lt;p&gt;&lt;a class=&quot;btn btn-lg btn-success&quot; href=&quot;http://www.yiiframework.com&quot;&gt;Get started with Yii&lt;/a&gt;&lt;/p&gt;


&lt;/div&gt;





&lt;div class=&quot;body-content&quot;&gt;





    &lt;div class=&quot;row&quot;&gt;


        &lt;div class=&quot;col-lg-4&quot;&gt;


            &lt;h2&gt;Heading&lt;/h2&gt;





            &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et


                dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip


                ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu


                fugiat nulla pariatur.&lt;/p&gt;





            &lt;p&gt;&lt;a class=&quot;btn btn-default&quot; href=&quot;http://www.yiiframework.com/doc/&quot;&gt;Yii Documentation &amp;raquo;&lt;/a&gt;&lt;/p&gt;


        &lt;/div&gt;


        &lt;div class=&quot;col-lg-4&quot;&gt;


            &lt;h2&gt;Heading&lt;/h2&gt;





            &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et


                dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip


                ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu


                fugiat nulla pariatur.&lt;/p&gt;





            &lt;p&gt;&lt;a class=&quot;btn btn-default&quot; href=&quot;http://www.yiiframework.com/forum/&quot;&gt;Yii Forum &amp;raquo;&lt;/a&gt;&lt;/p&gt;


        &lt;/div&gt;


        &lt;div class=&quot;col-lg-4&quot;&gt;


            &lt;h2&gt;Heading&lt;/h2&gt;





            &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et


                dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip


                ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu


                fugiat nulla pariatur.&lt;/p&gt;





            &lt;p&gt;&lt;a class=&quot;btn btn-default&quot; href=&quot;http://www.yiiframework.com/extensions/&quot;&gt;Yii Extensions &amp;raquo;&lt;/a&gt;&lt;/p&gt;


        &lt;/div&gt;


    &lt;/div&gt;





&lt;/div&gt;

</div>

This is the start script you need to use

127.0.0.1/advanced/frontend/web/index.php

If you are new to Yii, do not use the advanced application!

Use the basic application instead.

And, when you are using that, you need to inform your webserver where the document root is.

For the basic yii application, you need to point the document root at yourapplication/web - not the root of the site, the ‘web’ directory.

See the docs:

http://www.yiiframework.com/doc-2.0/guide-start-installation.html#verifying-installation

Even me, who knows Yii, am not using the advanced Yii application template. Simply because the basic does everything that I need it to do.

Even for really advanced applications.

Me too. :D

Alright, I install the basic application of Yii. I’m still getting the same error(HTTP 500 internal error).

I tried the ‘yii serve’ command in the ‘web’ directory and I got the following error:

PHP Warning: require(/var/www/html/basic/vendor/autoload.php): failed to open stream: No such file or directory in /var/www/html/basic/yii on line 14

PHP Fatal error: require(): Failed opening required ‘/var/www/html/basic/vendor /autoload.php’ (include_path=’.:/usr/share/php’) in /var/www/html/basic/yii on line 14

What do I do now?

I appreciate the help guys!

Danny

Is there a ‘vendor’ directory at the root of your basic Yii 2 application?

How did you install the basic Yii 2 application template?

I do have a vendor directory. When I run it, I get the following:

root@vagrant:/var/www/html/basic# php yii serve

Server started on http://localhost:8080/

Document root is "/var/www/html/basic/web"

Quit the server with CTRL-C or COMMAND-C.

I know this should work, but it’s not working still. I still have an HTTP 500 error.

I used ftp to send the ‘basic’ application files from my desktop to my server.

What else do I need to do?

Thanks!

Danny

Go to the ‘web’ directory and run this command:




php ../yii serve --port=8888



And then, visiting http://localhost:8888/ should hopefully work.

The problem could be that the default port - 8080 - is already in use on your system. That would explain the 500 server error.

The site is working! Thanks! Do you know of any good resources to learn Yii other than the Definitive Guide?

The TutsPlus tutorial is good: https://code.tutsplus.com/series/how-to-program-with-yii2--cms-725

And the Yii cookbook is also nice: https://github.com/samdark/yii2-cookbook

Thanks man! You’ve been such a big help!