tighten / ziggy

Use your Laravel routes in JavaScript.
MIT License
3.91k stars 248 forks source link

feat: expose route method #547

Closed jaulz closed 1 year ago

jaulz commented 2 years ago

This PR restructures Ziggy class so it can expose a route method (name is up to discussion) that returns a route in the same format as it is exposed in the generated file. In my use case I use it to expose the current route separately:

        $ziggy = new \Tightenco\Ziggy\Ziggy();

        $ziggy
          ->route($request->route())
          ->toArray();
bakerkretzmar commented 2 years ago

Thanks for the PR! Can you say more about what you would want to use this for exactly? This feels kind of out of scope for Ziggy, especially if you're doing it on the server since you can use Laravel's built-in routing features.

jaulz commented 2 years ago

To reduce the app bundle size I use a Babel macro that replaces route calls with the actual route definitions (e.g. route("home") will be compiled into { "uri": "...", "methods": [...], "bindings": [] } based on the generated JSON file by Ziggy). The only route that I would like to expose is the current route so I can easily manipulate the current URL via Ziggy. Consequently I need it in the format that is expected by the JavaScript class and thus I would simply use a method of this library instead of using my own custom function (that I might need to adapt if the library changes).

It is a special scenario but I think the changes of this PR would not add any complexity.

jaulz commented 2 years ago

@bakerkretzmar any chance to have another look? 😊 thanks in advance!

bakerkretzmar commented 2 years ago

@jaulz will revisit this again this week 👍🏻 this would accomplish more or less the same thing, right?

use Tightenco\Ziggy\Ziggy;

(new Ziggy)->toArray()['routes'][$request->route()];
jaulz commented 2 years ago

@bakerkretzmar yep, but since that would prepare all routes first and then only take a single one I thought it would make more sense to split the logic into two methods which also makes it easier to read in my opinion.

jaulz commented 2 years ago

@bakerkretzmar do you think you could have another look soon? Happy to implement any fix if necessary 😊

jaulz commented 1 year ago

@bakerkretzmar sorry but I will close the PR for the time being but thanks anyway for the great project! In the meantime I moved on and released a framework which is quite similar to Inertia but comes with a lot of new features including injected route definitions via Vite. In terms of route matching it's largely similar to your's, so thanks for that! You can find more details here: https://github.com/navigarejs/framework