Answer the question
In order to leave comments, you need to log in
Yii does not find the class in the controller. What could be the problem?
I'm trying to make a RESTFull API in Yii2. Before that, I did RESTFull only on Laravel. First I decided to try following the instructions https://www.yiiframework.com/doc/guide/2.0/ru/rest... . I do it in openserver. Installed the basic version. The index page opened, Gii also opened. Connected the database in the config. Next, following the instructions, I created the User controller in the C:\OSPanel\domains\testYii2\controllers folder:
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
]
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
]
$ curl -i " testyii2/users "
HTTP/1.1 200 OK
Date: Sat, 19 Dec 2020 14:56:56 GMT
Server: Apache
Content-Length: 2104
Content-Type: text/html; charset=UTF-8
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
public $modelClass = 'app\models\User';
}An Error occurred while handling another error:
yii\web\HeadersAlreadySentException: Headers already sent in C:\OSPanel\domains\testYii2\controllers\UserController.php on line 1. in C:\OSPanel\domains\testYii2\vendor\yiisoft \yii2\web\Response.php:370
Stack trace:
#0 C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\web\Response.php(343): yii\web\Response->sendHeaders()
#1 C:\OSPanel\domains\testYii2\vendor\yiisoft \yii2\web\ErrorHandler.php(136): yii\web\Response->send()
#2 C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\base\ErrorHandler.php(135): yii\ web\ErrorHandler->renderException(Object(yii\base\UnknownClassException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\base\UnknownClassException))
#4 {main}
Previous exception:
yii \base\UnknownClassException: Unable to find 'app\controllers\UserController' in file: C:\OSPanel\domains\testYii2/controllers/UserController.php. Namespace missing? in C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\BaseYii.php:296
Stack trace:
#0 [internal function]: yii\BaseYii::autoload('app\\controllers...')
#1 [internal function]: spl_autoload_call('app\\controllers...')
#2 C:\OSPanel\ domains\testYii2\vendor\yiisoft\yii2\base\Module.php(637): class_exists('app\\controllers...')
#3 C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\base\ Module.php(596): yii\base\Module->createControllerByID('user')
#4 C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\base\Module.php(522): yii\base\ Module->createController('index')
#5 C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\web\Application.php(104): yii\base\Module->runAction('user/index', Array)
#6 C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\base\Application.php(392): yii\web\Application->handleRequest(Object(yii\web\Request))
#7 C:\OSPanel\domains\testYii2\web\index.php(12): yii\base\Application->run()
#8 {main}
So it doesn't find the UserController?
Previous exception:
yii\base\UnknownClassException: Unable to find 'app\controllers\UserController' in file: C:\OSPanel\domains\testYii2/controllers/UserController.php. Namespace missing? in C:\OSPanel\domains\testYii2\vendor\yiisoft\yii2\BaseYii.php:296
Answer the question
In order to leave comments, you need to log in
<?php
namespace Foo;
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question