skip to Main Content

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

2

Answers


  1. 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 AppHttpControllersFeedbackController;
    use IlluminateRoutingRouter as RoutingEngine;
    use IlluminateSupportFacadesRoute;
    
    $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.

    Login or Signup to reply.
  2. You can use

    Route::get('{type}/report-feedback', [FeedbackController::class, 'feedbackForm'])
    ->where('type', 'users|chancellors')
    ->name('feedbackForm.get');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search