V
V
Vyacheslav2020-12-20 13:11:09
Yii
Vyacheslav, 2020-12-20 13:11:09

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';
    }


inserted
'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
    ],
]


and

'request' => [
    'parsers' => [
        'application/json' => 'yii\web\JsonParser',
    ]
]


to file C:\OSPanel\domains\testYii2\config\web.php

When I try to do

curl -i -H " Accept:application/json " " testyii2/users "

I get

$ 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


Could it be because of different slashes:

C:\OSPanel\domains\testYii2/controllers/UserController.php

How to solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bashcode, 2020-12-29
@bashcode

<?php
namespace Foo;
?>

It should look like this. There must be no extra characters between "namespace" and "<?php". Do not use the short "<?" tag.
Slash d.b. useless.
Perhaps the file encoding is not correct. You need to use UTF8 without BOM (if so, find yourself a decent editor).
Another option is the incorrect space app. See what is written in the "neighboring" controllers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question