saidbakr
3 years ago
1509
11
Laravel

How to get a route from URI

Posted 3 years ago by saidbakr

In /routes/web.php I have defined the application routes something like the following:

Route::get('/user','[email protected]');
Route::get('/user/edit/{id}','[email protected]');
Route::get('/user/create','[email protected]');
Route::post('/user/store','[email protected]');
Route::post('/user/update/{id}','[email protected]');

I want to a method that take an URI and then will return a route, hypothetically, getRouteOfUri($uri) which for example :

dd(getRouteUri('/user/update/2'));
/**
"HEADuser/edit/{id}" => Route {#209 ▼
      +uri: "user/edit/{id}"
      +methods: array:2 [▶]
      +action: array:6 [▶]
      +controller: null
      +defaults: []
      +wheres: []
      +parameters: null
      +parameterNames: null
      +computedMiddleware: null
      +compiled: null
      #router: Router {#21 ▶}
      #container: Application {#3 ▶}
    }
**/

Please sign in or create an account to participate in this conversation.