'Laravel php artisan "Class request does not exist"
I'm working with Laravel,
suddenly when I try to run php artisan serve in my command prompt,
it's displaying the error message:
In Container.php line 729: Class request does not exist
I have looked in all my controllers, models, and other files I could think of for loose Request references, but found nothing.
How do I debug this?
Solution 1:[1]
I was using request() in config.php, and I don't know why this works until some day that crash with this error. The site was working well, but composer/artisan commands fail. I think it's normal, because there is no request... I handled checking isset($_SERVER["SERVER_NAME"]), because I need the SERVER_NAME to config some parameters.
Solution 2:[2]
I struggled with this issue for a while.
Turns out, my issue was in a config file. One of my config classes was calling a static method with a bug in it.
The way I found it was to add echo statements in the following 2 files:
vendor/laravel/framework/src/Illuminate/Container.php
To echo the classes the container is building.
/**
* 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);
and here.
vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadConfiguratio n.php
To echo the config classes being loaded.
/**
* 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);
}
}
Solution 3:[3]
I was having this problem because a config file was calling the request helper function.
Since I did not use this configuration when running my application from console, I just checked if the request was running from console before using the request helper. For example,
# my config file
return [
'conf_key' => (app()->runningInConsole() === false) ? request()?->headers?->get('origin') : null,
// ...
Solution 4:[4]
Recheck your Controller class exists, because this error is thrown when there is some difference in Controller class name like
class PostController extends Controller { }
and
class Postcontroller extends Controller { }
Notice small "C"
Solution 5:[5]
In my case it was a problem with permission to bootstrap/cache on CI server. Check this directory twice.
Solution 6:[6]
I've created a cache directory in bootstrap directory and my problem solved
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lorenzo Belfanti |
| Solution 2 | Jon Winstanley |
| Solution 3 | Wesley Gonçalves |
| Solution 4 | Amandeep Singh |
| Solution 5 | GetoX |
| Solution 6 | sadegh zare |
