framework: [Proposal] Add Redirect::to()->withSuccess() - opposite to withErrors()

Hi All,

I was wondering if there was any interest in adding the ability to use withSuccess when redirecting. Support for this would also need to be added to ViewServiceProvider.php to make the success messages global over all views. This would let you build in to your views a global (or per view) ability to respond to a success message much as we can do with error messages right now.

Some example use cases:

Redirect::home()->withSuccess('Thanks for registering, please check your email for an activation link');
Redirect::home()->withSuccess('Activation complete');
Redirect::home()->withSuccess('Your password reset email has been sent');
Redirect::home()->withSuccess('Your password has been updated');

Then perhaps in your master layout file you could add (blade/bootstrap example):

@if($success->any())
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <h4>Success</h4>
        <ul>
            {{ implode('', $success->all('<li>:message</li>')) }}
        </ul>
    </div>
@endif

That would be optional of course, you could deal with it on a per view basis or not at all if you wanted.

Obviously this is possible with session flashing and then checking for the existence of the variable right now, but as we assured to always have an $errors MessageBag (empty or not) it makes sense to me to also have $success MessageBag too. This also reduces boilerplate on what must be a fairly common operation for a lot of users.

Thoughts?

Edit: I dont mind doing the PR for this if there is interest.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Reactions: 5
  • Comments: 27 (19 by maintainers)

Commits related to this issue

Most upvoted comments

@vedmant you can actually use with* and the data will be flashed to the session with * as key. (code) So ->withSuccess() works, it’s just not defined as an explicit method.

Is it worthwhile just making this as flexible as possible?

->with('errors', 'This is an error message') // can be aliased to ->withErrors()
->with('success', 'It works!');
->with('foo', 'This is a foo message');

All this is really doing under the hood is using flash messages, so its pretty easy to do this manually anyway.

Wow, didn’t expect you to answer me! I’m just talking that having withErrors() makes a lot of people think that something like withSuccess() exists, but it doesn’t. This is just how people think, they suppose if there is “A”, then it have to be and “B” as well, but in this case there is only “A” and it makes filling that something is not complete here. If there were no method withErrors(), people wouldn’t think that there may be something like withSuccess().

There is withErrors() method, I think it should have full functionality, if you are not going to implement methods like withSucces(), withNotice(), withInfo(), withWarning(), withPopup(), it’s better to remove withError() then. Make it consistent. IMHO.

+1, or rename the $errors to $messages