在第 1 行的 Psy Shell 代码中找不到 Laravel 5.6 Tinker Class '...'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51233169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.6 Tinker Class '...' not found in Psy Shell code on line 1
提问by Timkolm
I'm trying to make a simple class in a fresh-installed Laravel. For this I have created a folder app/Convert and put the php-file there. The class looks like this:
我正在尝试在新安装的 Laravel 中创建一个简单的类。为此,我创建了一个文件夹 app/Convert 并将 php 文件放在那里。这个类看起来像这样:
<?php
namespace App\Convert;
class Converter
{
public function __construct()
{
...Code
}
}
Now when I try to access it from Tinker:
现在,当我尝试从 Tinker 访问它时:
use App\Convert\Converter;
new Converter;
I get:
我得到:
PHP Fatal error: Class 'App/Convert/Converter' not found in Psy Shell code on line 1
What am I doing wrong? As I understand laravel should autoexecute the files inside the app folder and it's subfolders or am I mistaken?
我究竟做错了什么?据我了解,laravel 应该自动执行 app 文件夹及其子文件夹中的文件,还是我弄错了?
EDIT: Sorry the first time I wrote it wrong (I just played around so much with the namespaces so I took the wrong version). The problem was actually that the file name was something else than Converter.php, so when I changed it to Converter.php things started to change. I would set the Devon's answer as the correct answer if he put it as an answer not comment. So write it here:
编辑:对不起,我第一次写错了(我只是在命名空间上玩了很多,所以我拿错了版本)。问题实际上是文件名不是 Converter.php,所以当我将其更改为 Converter.php 时,事情开始发生变化。如果他将德文的答案作为答案而不是评论,我会将其设置为正确答案。所以写在这里:
Name of the file containing class should be the same as the class name. And the path to the file is the same as the namespace path.(see the Matthew's comment below his answer)
包含类的文件名应与类名相同。并且文件的路径与命名空间路径相同。(见马修在他的回答下面的评论)
回答by Ale DC
sometimes it is necessary to clear the cache, this command worked in my case:
有时需要清除缓存,此命令在我的情况下有效:
composer dump-autoload
I hope it works for you too
我希望它也适用于你
regards
问候
回答by Matthew Daly
Your namespace in the class differs from how you are trying to import it. You set it as App\Convert
there. You would need to import it as App\Convert\Converter
.
您在类中的命名空间与您尝试导入它的方式不同。你把它设置成App\Convert
那里。您需要将其导入为App\Convert\Converter
.
The fully qualified class name for a class includes the namespace AND the class name. So, for instance, if you have a class called Bar
with a namespace of App\Foo
, the fully qualified class name is App\Foo\Bar
, and to be able to use it as Bar
, you'd need to import it as follows:
类的完全限定类名包括命名空间和类名。因此,例如,如果您有一个Bar
命名空间为App\Foo
的类,则完全限定的类名称为App\Foo\Bar
,并且为了能够将其用作Bar
,您需要按如下方式导入它:
use App\Foo\Bar;
Also, as Devon said below, you'd need to have the file containing the class at app/Foo/Bar.php
for the default Laravel autoloader configuration to pick it up.
此外,正如德文在下面所说的,您需要拥有包含类的文件,app/Foo/Bar.php
用于默认 Laravel 自动加载器配置以获取它。