Laravel php artisan "Class request does not exist"
Asked Answered
C

7

6

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?

Christiniachristis answered 15/6, 2018 at 6:0 Comment(3)
post complete error log.Yet
I think some classes do not exist so remove composer.lock and vendor directory, then run composer install and test again.Homesteader
check your controller.php there may be Request reference in your action like this public function index(Request $request), for this you need to import use Illuminate\Http\Request;Biocatalyst
U
5

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,
// ...
Unhouse answered 31/3, 2022 at 16:4 Comment(0)
D
4

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.

Daudet answered 28/7, 2021 at 17:46 Comment(1)
Welcome to Stackoverflow. Please read how to write a good answer.Disulfide
R
2

I've created a cache directory in bootstrap directory and my problem solved

Retroaction answered 23/12, 2020 at 15:8 Comment(2)
Answers should include the exact code needed to solve the OP's problem.This is welcome info, but more appropriately left as a Comment.Heti
This solved my problem, and I have no idea why this would happen.Cozen
F
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);
    }
}
Flaggy answered 2/2, 2022 at 22:21 Comment(1)
So, what fix you did to resolve this?Homburg
Y
0

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"

Yet answered 15/6, 2018 at 6:38 Comment(0)
P
0

In my case it was a problem with permission to bootstrap/cache on CI server. Check this directory twice.

Phototherapy answered 31/8, 2020 at 12:8 Comment(0)
P
0

I just upgraded from laravel 8 to laravel 11 and encounter this problem I was having this problem because i was using request() helper in the App\Exception\Handler.php i have removed it and now the problem is solved

   $errorData = [
        'exception' => $exception,
        'url' => request()->fullUrl(),
        'user' => auth()->user(),
        'inputs' => request()->all(),
    ];
Pucka answered 24/5 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.