-1

I have same controller name(suppose Login.php) in 2 different folders in laravel 8 project. In route it is showing error if I use them in following way.

use App\Http\Controllers\my_controller1\Login;
use App\Http\Controllers\my_controller2\Login;

Error Looks Like

Symfony\Component\ErrorHandler\Error\FatalError

Cannot use App\Http\Controllers\my_controller2\Login as Login because the name is already in use

It will not show error If I use in following way:

use App\Http\Controllers\my_controller1\Login;

and

Route::get('/loginA',[App\Http\Controllers\my_controller2\Login::class,'abc']);

Route::get('/loginB',[Login::class,'abc']);

NOTE: Folder and Controller names are just for assumption.

user2729017
  • 77
  • 1
  • 8
  • 5
    you can name the controllers what you want, but your `use` statement is an alias so you are aliasing both of those controllers to `Login`, which you can't do as the name already exists after the first alias ... you would have to alias the second one to a different name – lagbox Oct 04 '20 at 17:50
  • did you change `namespace` for controller ? – نور Oct 04 '20 at 17:52
  • @lagbox There is 2 different `use` statements. `use App\Http\Controllers\my_controller1\Login;` and `use App\Http\Controllers\my_controller2\Login;` – user2729017 Oct 04 '20 at 17:53
  • 1
    again ... the `use` statement is an "alias" ... `use App\Http\Controllers\my_controller1\Login` IS `use App\Http\Controllers\my_controller1\Login as Login;` ... that is what that statement does ... so your use statements are both trying to alias the `Login` controllers in each to `Login`, which you can't do as after the first use statement `Login` is already used (as the error says) – lagbox Oct 04 '20 at 17:54
  • @noor Yes, 2 controller have different namespace – user2729017 Oct 04 '20 at 17:55

2 Answers2

4

There is 2 different use statements. use App\Http\Controllers\my_controller1\Login; and use App\Http\Controllers\my_controller2\Login;

You'll want to alias one of these if you're using them both in the same file.

use App\Http\Controllers\my_controller2\Login as Login2;
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • I works, but I want to ask why it is showing error when I use 2 different `use`(..\my_controller1\.. and ..\my_controller2\..) statement of different namespace. – user2729017 Oct 04 '20 at 18:08
  • @user2729017 i explained why in the comments – lagbox Oct 04 '20 at 18:15
4

You could give them different names by importing them like this

use App\Http\Controllers\my_controller1\Login as Login1;
use App\Http\Controllers\my_controller2\Login as Login2;

However, there should not be the need to have two controllers with the same name as it suggests that they are responsible for the same thing and probably could be merged.

Also, your naming seems a bit off; consider using camelCase for folder names (e.g. \customControllers\ instead of \my_controller1\) and naming controllers LoginController instead of Login only (see naming conventions for Laravel).

theovier
  • 184
  • 11
  • 1
    ok, but I use name just for example. This name is not my actual controller name and folder names. – user2729017 Oct 04 '20 at 18:01
  • 1
    You saved my day! I was converting Laravel 7 to Laravel 8. It helped me fixing using same controller under different folders (like admin, student etc.) – codewitharefin Dec 22 '20 at 16:16