Using "use Yii" within view crashes Apache

I’m trying to check if a user is a guest within a view and I tried this:




<?php


use Yii;


//........


?>


//....


<?php if (!Yii::$app->user->isGuest) : ?>

//......

<?php endif; ?>



…every time I refreshed the page Apache would crash.

So then I removed the use statement and did this instead:




<?php if (!\Yii::$app->user->isGuest) : ?>

//......

<?php endif; ?>



…and it worked fine.

Do you realize that its php statement and ought to be in <?php ?>

Sorry, yes it was within one. I have updated my post to reflect this.

put error message too. Its too important to be hidden!

I’m using Xampp locally (this is my dev environment) and there isn’t anything too specific in the logs. Here is what is logged when the error happens:




[Sun Dec 28 13:32:54.671875 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00428: Parent: child process exited with status 3221225477 -- Restarting.

[Sun Dec 28 13:32:55.015625 2014] [ssl:warn] [pid 5268:tid 316] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]

[Sun Dec 28 13:32:55.109375 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00455: Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4 configured -- resuming normal operations

[Sun Dec 28 13:32:55.109375 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00456: Server built: May 13 2012 14:10:15

[Sun Dec 28 13:32:55.109375 2014] [core:notice] [pid 5268:tid 316] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'

[Sun Dec 28 13:32:55.109375 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00418: Parent: Created child process 5500

AH00548: NameVirtualHost has no effect and will be removed in the next release C:/xampp/apache/conf/httpd.conf:293

[Sun Dec 28 13:32:56.015625 2014] [ssl:warn] [pid 5500:tid 1836] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]

[Sun Dec 28 13:32:56.125000 2014] [mpm_winnt:notice] [pid 5500:tid 1836] AH00354: Child: Starting 150 worker threads.

[Sun Dec 28 13:32:59.343750 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00428: Parent: child process exited with status 3221225477 -- Restarting.

[Sun Dec 28 13:32:59.656250 2014] [ssl:warn] [pid 5268:tid 316] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]

[Sun Dec 28 13:32:59.765625 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00455: Apache/2.4.2 (Win32) OpenSSL/1.0.1c PHP/5.4.4 configured -- resuming normal operations

[Sun Dec 28 13:32:59.765625 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00456: Server built: May 13 2012 14:10:15

[Sun Dec 28 13:32:59.765625 2014] [core:notice] [pid 5268:tid 316] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'

[Sun Dec 28 13:32:59.765625 2014] [mpm_winnt:notice] [pid 5268:tid 316] AH00418: Parent: Created child process 5844

AH00548: NameVirtualHost has no effect and will be removed in the next release C:/xampp/apache/conf/httpd.conf:293

[Sun Dec 28 13:33:00.531250 2014] [ssl:warn] [pid 5844:tid 1836] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]

[Sun Dec 28 13:33:00.640625 2014] [mpm_winnt:notice] [pid 5844:tid 1836] AH00354: Child: Starting 150 worker threads.



I didn’t mean server log but rather error you on your browser

There is no error. What I get is a Windows popup window that basically says "Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience." then I have the option of sending an error report to Windows or not.

Once I close it, the page stalls for awhile longer and then can’t be resolved. At that point I have to restart Apache.

Mhh! Strange! Did you try WAMP just in case its XAMMP failing to report error correctly?

Its silly suggestion but well, I cant think of anything else!

Naa… haven’t tried it as not too worried about it since it works normally with the workaround I used; just thought I would mention it.

Your logs are in ‘runtime’ dir. Check that YII_DEBUG is set to true to display errors.

I would assume this is an Apache error that Yii is causing somehow so thought it should be in Apaches logs? But either way, nothing was logged in the Yii logs.

There won’t be anything in apache’s log, unless Yii throws http exception explicitly. It should give you php warning, because the statement

makes no sense.

Debugging will help you, it is quite hard to develop an app blindfold.

What do you mean it makes no sense?

Classes from global space (e.g Yii class) are available in any namespace, they don’t have to be imported with ‘use’.

That’s not true. Since I wasn’t using a namespace in the view I didn’t need to use it, however you do need to do:


use Yii;

inside other files where you are in a different namespace.

Sorry, I mislead you, this needs an explanation. Technically, view file is yii\web\View (it is included from its method). Because of the way php treats inheritance in includes, $this is there but otherwise it doesn’t behave itself like it is object scope. Treat it like it is global space, so no ‘use’ of un-namespaced classes. Btw,


use Yii as Yii;

would be useless there also, but give no warning, that’s a known vagary of php.