【问题标题】:How can I prevent the error "the class name or namespace probably has a typo" in Symfony 4?如何防止 Symfony 4 中出现“类名或命名空间可能有错字”的错误?
【发布时间】:2018-07-06 08:43:08
【问题描述】:

我收到以下错误消息...

自动加载器预期的类“App\DataTables\UsersDataTable”是 在文件中定义 “/Users/work/project/vendor/composer/../../src/DataTables/UsersDataTable.php”。 找到文件但类不在其中,类名或 命名空间可能有错字 /Users/work/project/config/services.yaml(在资源中加载 “/Users/work/project/config/services.yaml”)。

但是我的 scr/DataTables/UsersDataTable.php 有一个正确的命名空间和类:

namespace App\DataTables\UsersDataTable;

use DataTables\DataTableHandlerInterface;
use DataTables\DataTableQuery;
use DataTables\DataTableResults;
use Symfony\Bridge\Doctrine\RegistryInterface;

class UsersDataTable implements DataTableHandlerInterface
{....

config/service.yaml 我这样命名...

datatable.users:
   class: App\DataTables\UsersDataTable
   tags: [{ name: datatable, id: users }]
   arguments: [ '@doctrine' ]

【问题讨论】:

    标签: symfony class namespaces yaml


    【解决方案1】:

    你快到了!
    命名空间语句表示“我在这个目录下”(好吧,命名空间)
    use 语句表示:加载这个(=一个文件)

    namespace App\DataTables;
    
    use DataTables\DataTableHandlerInterface;
    // [...]
    
    class UsersDataTable extends DataTableHandlerInterface
    {
    

    【讨论】:

    • 非常感谢!错误信息消失了。现在又出现了一个错误信息InvalidArgumentException Cannot determine controller argument for "App\Controller\DataTableController::usersAction()": the $request argument is type-hinted with the non-existent class or interface: "App\Controller\Request". Did you forget to add a use statement?但这和这个问题无关吧?
    • Did you forget to add a use statement? => 是的。你使用像 PHPstorm 这样的现代 IDE 吗?现代 IDE 软件包括自动类
    • 是的,我正在使用 Atom 编辑器
    • 我不使用Atom,但尝试在Request类上按ctrl + Enter或Ctrl +空格来自动包含use语句。
    • 嗯,我不知道具体怎么做,具体在哪里
    猜你喜欢
    • 1970-01-01
    • 2015-05-08
    • 2013-11-11
    • 2011-12-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    相关资源
    最近更新 更多