Uncaught Error quot;Class not foundquot; in Laravel 5.8(未捕获的错误“找不到类在 Laravel 5.8 中)
问题描述
我正在尝试从另一个文件夹调用我的模型文件.这两种文件结构我都提供了.
I am trying to call my model file from another folder. I have provided both of these file structure.
我收到此错误:
未捕获的错误:在/Applications/XAMPP/xamppfiles/htdocs/pro/app/Scripts/Providers/1/Scrape.php:17 中找不到类AppModelsProviders"
Uncaught Error: Class 'AppModelsProviders' not found in /Applications/XAMPP/xamppfiles/htdocs/pro/app/Scripts/Providers/1/Scrape.php:17
我正在从位于的脚本文件夹中调用模型文件:
I am calling the model file from a script folder located :
app/Scripts/Providers/1/Scrape.php
在这堂课中,我有以下内容:
In this class I have the below :
namespace AppScriptsProviders1;
use AppModels;
模型文件所在位置:
app/Models/Providers.php
在这个文件中,我有以下内容:
Within this file I have the below:
namespace AppModels;
use IlluminateDatabaseEloquentModel;
我没有分享我在这两个文件中的全部内容.如果您想查看这些文件的完整内容,请告诉我.
I have not shared the full content that I have in both of these files. If you would like to see the full content of these files please let me know.
这就是 Scrape.php 的样子
This is how the Scrape.php looks like
<?php
namespace AppScriptsProviders1;
use AppModelsProviders;
class Scrape {
public function __construct() {
$test = new AppModelsProviders();
die(print_r($test, true));
}
}
$obj = new Scrape();
推荐答案
你不能有一个以数字开头的命名空间.
You can't have a namespace that starts with a number.
命名空间遵循相同的变量名的基本规则:
有效的变量名称以字母或下划线开头,后跟任意数量的字母、数字或下划线
A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores
(强调我的).
因此,您的声明
namespace AppScriptsProviders1
基本上无效.
从那时起,所有赌注都取消了.
From that point forward, all bets are off.
首先,将您的命名空间更改为有效的标识符(我建议您选择比 数字 更合理和可识别的名称,您可以使用描述性名称,而这只是 没有理由 不):
First, change your namespace to a valid identifier (and I would advise choosing something more reasonable and recognizable than numbers, you can have descriptive names and there is simply no reason not to):
namespace AppScriptsProvidersGroupWhatever
从逻辑上讲,您必须重命名此文件所在的文件夹.以前是
Logically, you'll have to rename the folder where this file resides. It used to be
app/Scripts/Providers/1/Scrape.php
所以将该目录重命名为
app/Scripts/Providers/GroupWhatever/Scrape.php
(在这两种情况下,将 GroupWhatever
替换为对您的应用程序和域有意义的内容).
(In both cases, replace GroupWhatever
with something that makes sense for your application and domain).
从那时起,如果 AppModelsProviders
类存在于 app/Models/Providers.php
中,它应该可以工作.
From that point forward, if the class AppModelsProviders
exists at app/Models/Providers.php
, it should work.
可能存在的另一个问题是不太清楚 Scripts/Scrape.php
是什么或如何调用它.
Another problem that there could exist, is that is not very clear what Scripts/Scrape.php
is or how is it called.
如果你在 Laravel 中通过调用 Laravel 控制器或控制台应用程序执行 Scrape.php
,这应该可以工作.
This should work if you are executing Scrape.php
from within Laravel, by calling a Laravel controller or console application.
如果您直接调用此脚本(例如通过执行 php app/Scripts/Providers/1/Scrape.php
(或更正后的 app/Scripts/Providers/GroupWhatever/Scrape.php
) 这根本行不通,因为自动加载逻辑根本没有运行.
If you are calling this script directly (e.g. by executing php app/Scripts/Providers/1/Scrape.php
(or the corrected app/Scripts/Providers/GroupWhatever/Scrape.php
) this simply won't work, since the autoloading logic is not run at all.
如果您手动执行脚本,除了上述更改,您还需要包含 composer autoload 脚本,该脚本位于 vendor/autoload.php
.
If you are executing your script manually, on top of the above changes you need to include composer autoload script, which is located at vendor/autoload.php
.
基本上,将此行添加到 Scrape.php
顶部附近:
Basically, add this line close to the top of your Scrape.php
:
require_once dirname( __DIR__ ) . '/../../../vendor/autoload.php';
(我想我放置了适当数量的 go-up-one-dir 路径段,但请确保它与安装中的正确路径匹配).
(I think I put the appropriate amount of go-up-one-dir path-segments, but you make sure it matches the correct path in your installation).
一旦就位,自动加载器就会运行,并且会找到类.
Once that is in place, the autoloader will be run, and classes will be found.
这篇关于未捕获的错误“找不到类"在 Laravel 5.8 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未捕获的错误“找不到类"在 Laravel 5.8 中
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01