user1037607
user1037607

Reputation: 133

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?

Upvotes: 6

Views: 12705

Answers (7)

Gautam Yadav
Gautam Yadav

Reputation: 11

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(),
    ];

Upvotes: 0

Wesley Gonçalves
Wesley Gonçalves

Reputation: 2305

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,
// ...

Upvotes: 5

Jon Winstanley
Jon Winstanley

Reputation: 23311

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);
    }
}

Upvotes: 3

rbv79
rbv79

Reputation: 51

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.

Upvotes: 5

sadegh zare
sadegh zare

Reputation: 53

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

Upvotes: 2

Mateusz Przybylek
Mateusz Przybylek

Reputation: 5855

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

Upvotes: 0

Amandeep Singh
Amandeep Singh

Reputation: 274

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"

Upvotes: 0

Related Questions