英文:
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']);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论