英文:
How to add route prefix based on logged in user prefix in laravel?
问题
我可以理解你的需求,你想要在 web.php
中只写一次路由,同时保持在 users
和 chancellors
前缀下都能访问。你可以这样处理:
Route::prefix('users')->group(function() {
Route::get('/report-feedback', [FeedbackController::class, 'feedbackForm'])->name('users.feedbackForm.get');
});
Route::prefix('chancellors')->group(function() {
Route::get('/report-feedback', [FeedbackController::class, 'feedbackForm'])->name('chancellors.feedbackForm.get');
});
这样,在 users
和 chancellors
前缀下都可以使用 report-feedback
路由,分别拥有对应的路由名称 users.feedbackForm.get
和 chancellors.feedbackForm.get
。
英文:
I have a route of feedback which is accessible to both users
and chancellors
prefixes, but the issue is that I want to write it once in web.php
, now I have to write it multiple times in users
and chancellors
prefixes also I have to change the route name which I don't want to do. Is there any way to do this?
When I use it normally out of the users
and chancellors
prefixes it is accessible to both, but it lacks the users
and chancellors
prefixes. I want to write this route once and when the user logins with the users
or chancellors
prefixes such that the users/report-feedback
or chancellors/report-feedback
Route::get('/report-feedback', [FeedbackController::class, 'feedbackForm'])->name('feedbackForm.get');
答案1
得分: 1
如果你想要多条路由,你需要多次定义它们。你可以使用路由分组将路由放在一起,但预先定义回调函数作为参数:
use App\Http\Controllers\FeedbackController;
use Illuminate\Routing\Router as RoutingEngine;
use Illuminate\Support\Facades\Route;
$routes = function (RoutingEngine $router) {
$router->get('/report-feedback', [FeedbackController::class, 'feedbackForm'])
->name('feedbackForm.get');
};
Route::name("users.")->prefix("users")->group($routes);
Route::name("chancellors.")->prefix("chancellors")->group($routes);
这样将会创建一个名为 users.feedbackForm.get
的路由,对应URL为 users/report-feedback
,还会创建一个名为 chancellors.feedbackForm.get
的路由,对应URL为 chancellors/report-feedback
。
英文:
If you want multiple routes you'll need to define them multiple times. You can use route groups to put routes together, but pre-define the callback function used as the argument:
use App\Http\Controllers\FeedbackController;
use Illuminate\Routing\Router as RoutingEngine;
use Illuminate\Support\Facades\Route;
$routes = function (RoutingEngine $router) {
$router->get('/report-feedback', [FeedbackController::class, 'feedbackForm'])
->name('feedbackForm.get');
};
Route::name("users.")->prefix("users")->group($routes);
Route::name("chancellors.")->prefix("chancellors")->group($routes);
So this will create a route for the URL users/report-feedback
named users.feedbackForm.get
, and another for the URL chancellors/report-feedback
named chancellors.feedbackForm.get
.
答案2
得分: 0
你可以使用
Route::get('{type}/report-feedback', [FeedbackController::class, 'feedbackForm'])
->where('type', 'users|chancellors')
->name('feedbackForm.get');
英文:
You can use
Route::get('{type}/report-feedback', [FeedbackController::class, 'feedbackForm'])
->where('type', 'users|chancellors')
->name('feedbackForm.get');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论