Home » Php » php – Cannot use Illuminate\Routing\Controller as Controller because the name is already in use

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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!

How to&Answers:

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
}