我正在与 Laravel 合作,
突然,当我尝试在命令提示符中运行
php artisan serve
时,
它显示错误消息:
In Container.php line 729: Class request does not exist
我查看了所有控制器、模型和其他我能想到的松散请求引用文件,但什么也没找到。
如何调试这个?
我遇到这个问题是因为配置文件正在调用
request
辅助函数。
由于我在从控制台运行应用程序时没有使用此配置,因此我只是在使用
request
帮助程序之前检查请求是否从控制台运行。例如,
# my config file
return [
'conf_key' => (app()->runningInConsole() === false) ? request()?->headers?->get('origin') : null,
// ...
我在
request()
中使用 config.php
,我不知道为什么它会起作用,直到有一天因为这个错误而崩溃。该网站运行良好,但作曲家/工匠命令失败。我认为这很正常,因为没有请求...我处理了检查isset($_SERVER["SERVER_NAME"])
,因为我需要SERVER_NAME
来配置一些参数。
我在 bootstrap 目录中创建了一个缓存目录,我的问题解决了
我为这个问题苦苦挣扎了一段时间。
事实证明,我的问题出在配置文件中。我的一个配置类正在调用一个带有错误的静态方法。
我发现的方法是在以下2个文件中添加echo语句:
vendor/laravel/framework/src/Illuminate/Container.php
回显容器正在构建的类。
/**
* Instantiate a concrete instance of the given type.
*
* @param string $concrete
* @return mixed
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function build($concrete)
{
// If the concrete type is actually a Closure, we will just execute it and
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this, $this->getLastParameterOverride());
}
echo 'Build - ' . $concrete . PHP_EOL;
$reflector = new ReflectionClass($concrete);
还有这里。
vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguratio n.php
回显正在加载的配置类。
/**
* Load the configuration items from all of the files.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Contracts\Config\Repository $repository
* @return void
* @throws \Exception
*/
protected function loadConfigurationFiles(Application $app, RepositoryContract $repository)
{
$files = $this->getConfigurationFiles($app);
if (! isset($files['app'])) {
throw new Exception('Unable to load the "app" configuration file.');
}
foreach ($files as $key => $path) {
echo 'config - ' .$key . PHP_EOL;
$repository->set($key, require $path);
}
}
重新检查您的 Controller 类是否存在,因为当 Controller 类名称存在一些差异时会抛出此错误,例如
class PostController extends Controller { }
和
class Postcontroller extends Controller { }
注意小“C”
就我而言,这是 CI 服务器上
bootstrap/cache
权限的问题。检查此目录两次。
我刚刚从 laravel 8 升级到 laravel 11 并遇到这个问题 我遇到这个问题是因为我在 App\Exception\Handler.php 中使用 request() 帮助程序 我已将其删除,现在问题已解决
$errorData = [
'exception' => $exception,
'url' => request()->fullUrl(),
'user' => auth()->user(),
'inputs' => request()->all(),
];