Cannot use Illuminate\Routing\Controller as Controller because the name is already in use

18,866

Solution 1

The use Illuminate\Routing\Controller; statement is failing because there is already a Controller class in the App\Http\Controllers namespace.

To solve the immediate issue, you can change namespace shortcut on the use statement:

use Illuminate\Routing\Controller as BaseController;

However, the solution for your specific issue is that you probably just want to remove the use Illuminate\Routing\Controller; statement altogether.

In Laravel 5, the App\Http\Controllers\Controller class already extends the Illuminate\Routing\Controller class. The intention is that all new controllers should then extend the App\Http\Controllers\Controller class. For example, take a look at the default App\Http\Controllers\HomeController or App\Http\Controllers\WelcomeController, as both extend the App\Http\Controllers\Controller class.

In summary, your two options are:

// rename the class in the use statement
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;

// note the name of the class being extended
class VarController extends BaseController {
    // snip
}

Or

// extend the existing App\Http\Controllers\Controller class
namespace App\Http\Controllers;

class VarController extends Controller {
    // snip
}

Solution 2

This is because of a duplicate that exists enter image description here

Solution is to remove the duplicate from your import enter image description here

See attached sample

Share:
18,866

Related videos on Youtube

Marcus Ruddick
Author by

Marcus Ruddick

Updated on June 04, 2022

Comments

  • Marcus Ruddick
    Marcus Ruddick almost 2 years

    I have been learning to use Laravel, watching Larcasts and using the Docs, I came across a lesson where Eloquent is being described but I'm stuck with the error:

    at HandleExceptions->fatalExceptionFromError(
      array(
        'type' => '64',
        'message' => 'Cannot use Illuminate\Routing\Controller as Controller because the name is already in use'
      )
    )
    

    I'm very confused and have now copied the examples provided exactly but I still get the error. I am using Laravel 5, so I don't know if there has been some undocumented change or If I am simply doing something wrong. I haven't found anything related in google searches that solve the issue so I was hoping someone here might be able to help. Here is the code that is producing the error:

    <?php namespace App\Http\Controllers;
    
    use Illuminate\Routing\Controller;
    
    use App\VarName;
    
    class VarController extends Controller {
    
        public function Var()
        {
            $Variable = VarName::get();
    
            dd($Variable);
        }
    }
    

    According to the documentation, this should work, and in the video that I watched, it did work.. what am I missing?

    I tried deleting the Controller class, since it seems to be whats causing the already in use error, which broke everything, reinstalled and tried to just use Controller since it extends the eloquent model but now its saying:

    ErrorException in Pluralizer.php line 258: call_user_func() expects parameter 1 to be a valid callback, function mb_strtolower not found or invalid function name

    which is beyond my understanding of the inner workings of Laravel, I'm stuck and I don't understand the problem, according to documentation I don't see anything wrong with my code, this seems like such a simple step. all I'm trying to do is retrieve info from a database, what is going on?

    Thanks in advance for any help!

    • Wrikken
      Wrikken over 9 years
      Have you defined an App\Http\Controllers\Controller class?
    • Marcus Ruddick
      Marcus Ruddick over 9 years
      There is a default Controller class, I did not create it but its there.
    • Marcus Ruddick
      Marcus Ruddick over 9 years
      does that have something to do with the problem I'm encountering?
  • Marcus Ruddick
    Marcus Ruddick over 9 years
    thank you for your answer, I kinda just gave up on trying Laravel 5 due to this, decided to wait till it was a stable release and had specific documentation regarding its differences from 4. have been using 4 without issue and I think I will still wait till 5 is stable, but good to know for future reference!
  • itsazzad
    itsazzad about 9 years
    It seems to be a bug in the :generate in laravel 5