christopher's avatar

Class 'App\Http\Controllers\Validator' not found

If i want to validate and store a new record i get the following error:

Class 'App\Http\Controllers\Validator' not found

But in my controller i have the class:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Http\FormRequest;
use App\Company;
use App\Country;
use App\User;

why am i getting this error ?

This is my store method

public function store()
    {
        $rules = array(
            'name'    => 'required',
        );

        $validator = Validator::make(Input::all(), $rules);

        // if the validator fails, redirect back to the form
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator) // send back all errors to the login form
                ->withInput();

            $input = input::all();

        } else {

            $company                = New Company();
            $company->name          = Input::get('name');
            $company->user_id       = Input::get('user_id');
            $company->country_id    = Input::get('country_id');
            $company->description   = Input::get('description');

            $company->save();

            return Redirect::to('/backend')->withInput()->with('success', Company added.');

        }
    }
0 likes
17 replies
christopher's avatar

odd ... now i`m getting

Class 'App\Http\Controllers\Input' not found
RachidLaasri's avatar

You may also need to

use Illuminate\Support\Facades\Redirect;

Because you are using Redirect facade.

3 likes
xingfucoder's avatar

Hi the solution is that @RachidLaasri gives you, you have two options:

  1. Prefix the class name with \ (ie. \Validator) as follow:

    public function store()
    {
        $rules = array(
            'name'    => 'required',
        );
    
        $validator = \Validator::make(Input::all(), $rules);
    
        //.....
    
  2. Imports the namespaces for Validator and the another classes.

1 like
acasar's avatar

You don't need to reference the whole path when using Facades. You can just do:

use Validator, Input, Redirect; 
1 like
christopher's avatar

Thanks for all your answers.

For me i thin kthe last method of @anzze seems quite good. How are you guys handle this ?

1 like
RachidLaasri's avatar

Both ways are good, i am using PHPStorm so i don't actually write the full path, it import them automatically for me.

If you are using something like SublimeText and you write them yourself, using this is a lot quicker.


use Validator, Input, Redirect;
1 like
christopher's avatar

I`m also using PHPStorm - But of course you have to know which things you have to put in :) :)

RachidLaasri's avatar

Yes, and PHPStorm can help you with that, when you hover over a class, you can see "Undefined class -name-".

So you know that you need to import it, also if you imported a class and never used it PHPStorm says "Alias -classname' is never used".

1 like
nolros's avatar

Sorry to hick jack this post, but I was having this facade discussion last night. Why you not a fan of facades, bad pattern @RachidLaasri ?

btw, we need some sort of DM on this site :)

RachidLaasri's avatar

@nolros

This "facade discussion" is a very complicated topic, there's hundreds discussions on the internet.

But to me, I think that Laravel Facades ( even if it's not the real facade pattern ) help you keep you code simple, clean and still testable by mocking facades. But what i find hard is to tell what are my controllers dependencies. while using dependency injection makes it a lot more clear to list my controller dependencies because they're injected on my constructor. But to be honest, it depends on you and which method you prefer to use.

ruturajh's avatar

I am new to laravel and I have a doubt - Why do we need to use these statements

use Validator, Input, Redirect;

if we have already mentioned these aliases in config/app.php ?

Please or to participate in this conversation.