I am new on PHP and currently I'm using Laravel 12 with InertiaJS and React, such toolkit allows me to use ziggy as a route helper and do things like:
import { RouteList } from 'ziggy-js';
type Routes=keyof RouteList;
type NavigationItem = {route_names:Routes[];key:string}
const retrieveActiveRoute = (navigationItems: NavigationItem[] ): string => {
const current_route = route().current() as keyof RouteList;
if (!current_route) return '0';
for (const { route_names, key } of navigationItems) {
const existentRoute = route_names.includes(current_route);
if (existentRoute) {
return key;
}
}
return '0';
};
The routeList created by the ziggy helper can be found by using the command
php artisan route:list
While I can create two routes with different methods, I don't want to give two different names for things that should have the same name, like a route that I want to use both POST and GET methods.
//...previous routes
Route::GET('/invoices',[CustomController::class,'custom_method'])->name('invoices.index');
Route::POST('/invoices',[CustomController::class,'custom_method'])->name('invoices.index');
The problem is that my retrieveActiveRoute function doesn't work when I use the 'invoices.index' name even while using the GET method.
I use this code on typescript like this:
const exampleNavigationItems:NavigationItem[]=[
{
route_names:['home'],
key:'1'
},
{
route_names:['invoices'],
key:'2'
},
];
const currentRouteKey = retrieveActiveRoute(exampleNavigationItems)
console.log(currentRouteKey);
This code should log into the console '2' but it gives '0'