skip to Main Content

In my api.php file, I might have a path that says Route::post('/this/thing/{variable}'

In another part of my codebase, when a request is made to that route, I can get the actual requested route with $request->path();, but if I do that, {variable} is of course replaced by whatever the request route actually put in there.

I want a $request->path() alternative that gives me the string '/this/thing/{variable}' without replacing {variable}, is that possible?

2

Answers


  1. $routePath = $request->route()->uri();
    

    This method returns the route URI as it was defined in the routes file

    Login or Signup to reply.
  2. Yes, you can get the route definition (including the placeholder like {variable}) using the following:

    $route = $request->route()->uri();
    

    This will give you the route URI with the placeholders ({variable}), instead of the actual value passed in the request.

    Route::post('/this/thing/{variable}', function (Request $request) {
      $routeDefinition = $request->route()->uri(); 
      return $routeDefinition; // returns '/this/thing/{variable}'
    });
    

    This will return the original route string with {variable} as part of it, which is what you are looking for.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search