route().current() returning 'undefined' for form POST routes
See original GitHub issueHello - first off, great package, thanks so much!
I have encountered an issue where the current route value for POST routes always comes back ‘undefined’. This does not happen for my GET routes, including when I change an undefined POST route into a GET route for testing purposes. I can also change a working GET route to POST and it always returns undefined. These routes are all defined in my web.php file using the ‘web’ middleware grouping.
I also tried adding one of my POST routes to the array of excluded URIs in VerifyCsrfToken in case it was a glaring CSRF token issue, but that did not change any behavior.
Expected Behavior
route().current()
should provide me with the name of my POST routes, as it does for GET routes.
Current Behavior
route().current()
returns ‘undefined’ for POST routes.
For bug reports only please provide
Currently installed Laravel version:
5.4.36
Currently installed Ziggy version
0.7.1
Example route from your Laravel Routes file.
i.e. -
Route::post('studySetup', 'PathologyController@studySetup')->name('studySetup');
Contents of Ziggy.namedRoutes
assignStudies: {uri: "assignStudies", methods: Array(2), domain: null}
checkInOut: {uri: "/", methods: Array(2), domain: null}
debugbar.assets.css: {uri: "_debugbar/assets/stylesheets", methods: Array(2), domain: null}
debugbar.assets.js: {uri: "_debugbar/assets/javascript", methods: Array(2), domain: null}
debugbar.clockwork: {uri: "_debugbar/clockwork/{id}", methods: Array(2), domain: null}
debugbar.openhandler: {uri: "_debugbar/open", methods: Array(2), domain: null}
dev_connections: {uri: "dev/connections", methods: Array(2), domain: null}
downloadTables: {uri: "downloadTables", methods: Array(2), domain: null}
globalChanges: {uri: "globalChanges", methods: Array(2), domain: null}
importStudies: {uri: "importStudies", methods: Array(2), domain: null}
managePermissions: {uri: "managePermissions", methods: Array(2), domain: null}
ntpPathologistReview: {uri: "ntpPathologistReview", methods: Array(2), domain: null}
previousChanges: {uri: "previousChanges", methods: Array(2), domain: null}
qaPathologistReview: {uri: "qaPathologistReview", methods: Array(2), domain: null}
selectionCriteria: {uri: "selectionCriteria", methods: Array(1), domain: null}
standardAnimalSelection: {uri: "standardAnimalSelection", methods: Array(2), domain: null}
studySetup: {uri: "studySetup", methods: Array(1), domain: null}
updateCheckInOutData: {uri: "updateCheckInOutData/{study_number}", methods: Array(2), domain: null}
__proto__: Object
Ziggy call in context
i.e. –
$(document).ready(function() {
...
// Page-specific document ready functionality
switch (route().current()) {
case 'studySetup':
console.log("Hooray, we made it to studySetup!");
break;
default:
console.log("Not studySetup - route.current() = " + route().current());
break;
}
});
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
I’m posting this here in case it helps others - a simple trick to use the package with POST routes is to call .url() on the route object.
axios.post(route('post.store').url(), { ... })
Thanks for the great package!
hi @kcchurch
to be clear how the current function works: It is looking for get routes only.
So when you want to check for a post route you need a second get route with the same url.
Otherwise you can fork the repo and change one specific line: https://github.com/tightenco/ziggy/blob/253b919cddc196a5a9a94c5d1e406d1b57f700f1/src/js/route.js#L123 There you see the
indexOf('GET')
which is your “problem”.