Getting Laravel Named Route By Router Helper Method In React JS
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?
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.
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?