在Laravel中如何基于已登录用户前缀添加路由前缀?

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

How to add route prefix based on logged in user prefix in laravel?

问题

我可以理解你的需求,你想要在 web.php 中只写一次路由,同时保持在 userschancellors 前缀下都能访问。你可以这样处理:

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');
});

这样,在 userschancellors 前缀下都可以使用 report-feedback 路由,分别拥有对应的路由名称 users.feedbackForm.getchancellors.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');

huangapple
  • 本文由 发表于 2023年6月30日 00:47:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583076.html
匿名

发表评论

匿名网友

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

确定