I've been trying to use Laravel's Route::getRoutes() method to generate a dynamic sitemap. The problem is that that method returns routes that are in reality redirects:
Example web.php:
Route::get('not-redirected', function() {
return view('not-redirected');
}
Route::get('redirected', function() {
return redirect('was-redirected');
}
Route::getRoutes() returns ['not-redirected', 'was-redirected']. Is there a way to dynamically filter out redirected routes from this output?
My desired output for the above web.php would be ['not-redirected'].
EDIT: I've noticed that php artisan route:list does not return redirects. However, I can't figure out why just by looking through the source code... It looks like it calls that same function, Route::getRoutes().
It seems there is no way to determine the return for a given route. The solution I came up with is to add a group all the routes that are 'special', this way you can easily add custom attributes to a route's action.
Route::group(['return_type' => 'redirect'], function () {
Route::get('not-redirected', function () {
return view('not-redirected');
});
Route::get('redirected', function () {
return redirect('was-redirected');
});
});
Then you could loop over the routes like this:
$routes = Route::getRoutes()->get();
foreach ($routes as $route) {
if ($route->getAction('return_type') === 'redirect') {
// Or whatever you want to do in this case
continue;
}
}