D
D
Dmitry Kim2016-09-21 08:22:57
Yii
Dmitry Kim, 2016-09-21 08:22:57

Why is className() method in yii2-basic not working properly?

Installed the app yii2-basicfrom scratch. In the config I web.phpchange the line:
It was:

'user' => [
  'identityClass'   => 'app\models\User',
  'enableAutoLogin' => true,
],

It became:
'user' => [
  'identityClass'   => app\models\User::className(),
  'enableAutoLogin' => true,
],

As a result, error 500. In the logs
PHP Fatal error:  Class 'app\\models\\User' not found in ...

And in yii2-advanced this works without dancing.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-09-21
@kimono

in Yii advanced, basic aliases are pre-installed, i.e. approximately the scheme is as follows:
1. Install aliases
2. Load the configuration
3. Launch the application with the loaded configuration
Accordingly, at the time of loading the configuration, php already knows which alias which path belongs to. In basic, there are no pre-installation of aliases, i.e. the configuration is loaded, then the application is launched, during the launch of which aliases are installed. Therefore, at the time of loading the config, php simply does not yet know what app is. Those. You are accessing the class app\models\User::className() but php doesn't know what app is at this point, because this alias is not yet defined.
You can define aliases yourself before the application is loaded using the Yii::setAlias ​​method

I
Igor Vasiliev, 2017-06-27
@Isolution666

Hello.
-
Another surprise awaits you. The fact is that the developers of the Yii2 Framework solved the issue with the loads on the site, and they had to abandon the ::className(), since php7 already has a solution for referring to classes, the whole structure will be replaced by ::class().
So, even if you resolve the alias issues, your classes may stop working again. Personally, I find this appeal to classes convenient, the code is noticeably reduced, and according to the debugger, everything is much better ))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question