如何避免 Laravel 控制器冲突

huangapple go评论72阅读模式
英文:

How to avoid laravel controller conflicts

问题

Sure, here's the translated code you're expecting:

use App\Http\Controllers\Deals as Deals;
use App\Http\Controllers\Admin\Deals as AdminDeals;
 
Route::get('/deals', [Deals::class, 'show']);
Route::get('/admin/deals', [AdminDeals::class, 'show']);

If you have any more code that needs translation, feel free to ask.

英文:

My laravel route file has two different controllers with same name but in different folder.

use App\Http\Controllers\Deals;
use App\Http\Controllers\Admin\Deals;
 
Route::get('/deals', [Deals::class, 'show']);
Route::get('/admin/deals', [Deals::class, 'show']);

Is there a way possible to rewrite the code and avoid conflict?

What I am expecting

use App\Http\Controllers\Deals as Deals;
use App\Http\Controllers\Admin\Deals as AdminDeals;
 
Route::get('/deals', [Deals::class, 'show']);
Route::get('/admin/deals', [AdminDeals::class, 'show']);

答案1

得分: 0

是的,您上面写的方式会起作用,但您需要更改别名或删除它,以便它变成这样:

use App\Http\Controllers\Deals;
use App\Http\Controllers\Admin\Deals as AdminDeals;

Route::get('/deals', [Deals::class, 'show']);
Route::get('/admin/deals', [AdminDeals::class, 'show']);

或者您始终可以使用绝对路径来避免名称冲突:

Route::get('/deals', [\App\Http\Controllers\Deals::class, 'show']);
Route::get('/admin/deals', [\App\Http\Controllers\Admin\Deals::class, 'show']);
英文:

Yes, the way you wrote above will work, but you need to either change the alias or remove it, so that it turns out like this:

use App\Http\Controllers\Deals;
use App\Http\Controllers\Admin\Deals as AdminDeals;

Route::get('/deals', [Deals::class, 'show']);
Route::get('/admin/deals', [AdminDeals::class, 'show']);

Or you can always use an absolute path to avoid name conflict:

Route::get('/deals', [\App\Http\Controllers\Deals::class, 'show']);
Route::get('/admin/deals', [\App\Http\Controllers\Admin\Deals::class, 'show']);

huangapple
  • 本文由 发表于 2023年6月29日 23:49:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定