Hello! I'd like to create something that allows me to save messages in the default flash session storage and display them on the next page request. It should allow for multiple messages, something like: Notify::add('errors', 'this is not right'); Notify::add('errors', 'this is not right, either');
Also, I'd like it to use the same messaging format as Laravel's built-in system, so I can still validate forms and redirect withErrors() and use the same view to display them.
I tried adding a service provider which registers a singleton like this:
public function register()
{
$this->app->singleton('Notify', function ($app) {
return new \App\Helpers\NotifyHelper();
});
}
NotifyHelper.php:
class NotifyHelper extends MessageBag {
public function add($level='info', $message) {
//code to add and flash data
}
}
Calling Notify::add() will result in a "Class not found" error. I'm adding the provider in config/app.php, of course. I tried looking around but I can't figure it out.
I think my final goal would be something like this: http://packalyst.com/packages/package/hampel/alerts but I'd like to try and do it myself, the simplest possible way.
Any hints? Thanks.
I'm pretty sure you need to build and register your own Facade class to Notify::add()
like that.
Otherwise, you would need to type-hint a Container-resolved object (such as a controller) which will then use method injection to deliver an instance of your class:
// controller method
function doThing(NotifyHelper $notify)
{
$notify->add('make sense?');
}
PS you have your optional arguments the wrong way round in that example.
Thank you, that's what I needed. I ended up leaving MessageBag alone and creating my standalone class which will merge errors from Laravel, it seems Laravel only uses 'errors' anyway.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community