Reputation: 28
In laravel we can use with()
along with redirect()
, like
return redirect('home')->with(['message' => 'Some message');
I want to create some other functions like withError()
, withSuccess()
.
How and where to create this ?
Upvotes: 0
Views: 649
Reputation: 9465
As the Laravel RedirectResponse class uses the Macroable trait, you can register response macros to do this.
Just create a new service provider say ResponseMacroServiceProvider
. Register it in your app.php
and register a macro in the boot method like so:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Http\RedirectResponse;
class ResponseMacroServiceProvider extends ServiceProvider
{
/**
* Register the application's response macros.
*
* @return void
*/
public function boot()
{
RedirectResponse::macro('withError', function ($value) {
return; // add logic here
});
RedirectResponse::macro('withSuccess', function ($value) {
return; // add logic here
});
}
}
Upvotes: 3