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

[5.2] Fallback parameter for UrlGenerator::previous #14207

Merged
merged 3 commits into from
Jul 18, 2016
Merged

[5.2] Fallback parameter for UrlGenerator::previous #14207

merged 3 commits into from
Jul 18, 2016

Conversation

CupOfTea696
Copy link
Contributor

Allows passing a fallback value to the UrlGenerator::previous() method. This parameter is optional, and if not provided will still fall back to UrlGenerator::to('/').

@taylorotwell
Copy link
Member

This has come up before and I still stand by my case of if you need a fallback then the whole point of this method is useless and shouldn't be used at all.

Is there a strong reason you need a fallback?

@GrahamCampbell GrahamCampbell changed the title Fallback parameter for UrlGenerator::previous [5.2] Fallback parameter for UrlGenerator::previous Jul 2, 2016
@CupOfTea696
Copy link
Contributor Author

I'll give you a use case:

In an administrator area, there is a page listing Models of some sort in a table. Next to each item you have an edit button. Each item can be clicked, directing the administrator to the single Model page. Each Model page has an edit button. The edit form has a cancel button.

When the user clicks the edit button from the list page and then clicks the cancel button, the user should return to the list page. When the user clicks the edit button on the Model page and then clicks the cancel button, the user should return to the Model page. Finally, if the user visited the edit page directly, the user should return to the list page.

In the last scenario, the user would be redirected to the root url when I use the UrlGenerator::previous() method.

{
$referrer = $this->request->headers->get('referer');

$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();

return $url ?: $this->to('/');
return $url ?: (value($fallback) ?: $this->to('/'));
Copy link
Member

Choose a reason for hiding this comment

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

I don't like nested ternarys.

@taylorotwell taylorotwell merged commit 56a2a7f into laravel:5.2 Jul 18, 2016
@vlakoff
Copy link
Contributor

vlakoff commented Jul 18, 2016

Note that with this code, getPreviousUrlFromSession has precedence over the provided fallback.

Refs #6293, #10229.

@taylorotwell
Copy link
Member

@vlakoff can you explain?

@vlakoff
Copy link
Contributor

vlakoff commented Jul 18, 2016

If there is no referer and a fallback is provided, getPreviousUrlFromSession will still be tried before using the fallback. A different approach could be to directly use the fallback, which might be better as it is more predictible.

@vlakoff
Copy link
Contributor

vlakoff commented Jul 18, 2016

In addition, $fallback should be wrapped in a $this->to() instead of being used as-is.

@taylorotwell
Copy link
Member

It is wrapped in a to when I merged it. I modified the PR.

@vlakoff
Copy link
Contributor

vlakoff commented Jul 19, 2016

Got it: a8ca2b7

Thoughts about my previous comments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants