Ad

Getting Laravel Named Route By Router Helper Method In React JS

- 1 answer

This is a quick question. I have been finding a way to do it for a while. I am developing Laravel application. I am using React JS for front end. What I like to do it I like to get the Laravel named route by calling Laravel route() helper method. There is a library/ package to do it in Vue JS. Is there a library to do it as well in React JS? Otherwise, what would be the best way?

Ad

Answer

There is no "package to do it in VueJS". There is a package exporting the route names and parameters for consumption in a test/PoC application. Those are two totally different scopes. Most people do not require this level of customizability as API routes are considered stable references.

If you took the time to establish a swagger file, their codegen tool can generate a full API client.

If you didn't, all hope is not lost, but you lose some details in the process. Laravel has a route collection, which you can work on relatively easy:

$routeCollection = Route::getRoutes();

foreach ($routeCollection as $value) {
   // $value is now a Route object
}

From this, you can easily write a controller method to export, in your preferred format, the information you need. Do note, however, that it is impossible (AFAIK) to get named route names from the router - you can only match a route by a name.

Ad
source: stackoverflow.com
Ad