Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a literal string from route() when generating a URL #336

Merged
merged 4 commits into from
Oct 23, 2020

Conversation

bakerkretzmar
Copy link
Collaborator

@bakerkretzmar bakerkretzmar commented Oct 9, 2020

This change would make using the route() function a little cleaner by removing the need to ever call .url(), bring its behaviour closer to matching Laravel's route() helper, and resolve a very annoying quirk with axios that required calling .url() to ensure that CSRF tokens are attached to the request.

When no arguments are passed to route(), it will continue to return a Router instance:

route(); // returns a Router
route().current(); // unchanged, returns a string (a route name)
route().current('home'); // unchanged, returns a boolean
route().params; // unchanged, returns an object
route().has('posts.show'); // unchanged, returns a boolean

However, now, if it's passed a name argument, it generates the URL and returns a string directly:

// before
route('home'); // returns a Router, which extends the String class, with a value of 'https://example.com'
route('home').url(); // returns 'https://example.com'

// after
route('home'); // returns 'https://example.com'

This PR also:

Removes the .with() method

This method was going to be deprecated in 1.0 since it isn't really necessary, parameters should always be passed as the second argument to route().

Removes the .withQuery() method

Query parameters can also be passed in the second argument to route(), mixed in with regular route parameters.

For edge cases like where a query parameter has exactly the same name as a route parameter, this PR adds a special _query parameter that will be added directly to the query string:

route('posts.show', {
    post: 4,
    _query: {
        post: 5,
    },
});
// 'https://example.com/posts/4?post=5'

Removes the .url() method

Calling .url() is no longer possible or necessary since route(...) with any arguments returns a string. To reduce repetition it was removed and its functionality was moved to the existing .toString() method on the Router class.

@bakerkretzmar bakerkretzmar modified the milestones: v1.1, v1.0 Oct 23, 2020
Copy link
Collaborator

@jakebathman jakebathman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I'm excited about this one 👍

@bakerkretzmar bakerkretzmar merged commit b4fd5ce into develop Oct 23, 2020
@bakerkretzmar bakerkretzmar deleted the jbk/return-string branch October 23, 2020 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants