An Introduction to Laravel Message Bags

November 29, 2016 —John Koster

The Illuminate\Suport\MessageBag class is an elaborate key/value storage system for storing different types of messages. It it also allows developers to specify a format which is used when returning the messages. The format makes it incredibly simple to format messages on HTML forms, emails, etc. The MessageBag class implements both the Illuminate\Contracts\Support\MessageProvider and Illuminate\Contracts\Support\MessageBag interfaces.

#Illuminate\Contracts\Support\MessageProvider Interface

The MessageProvider interface defines only one method, getMessageBag. The expected behavior of the getMessageBag method is to return an instance of an implementation of the Illuminate\Contracts\Support\MessageBag interface. Any class that implements the MessageProvider interface can be expected to be capable of returning MessageBag implementations.

The following classes extend, implement, or make use of the MessageProvider interface by default:

Class Description
Illuminate\Contracts\Validation\ ValidationException The ValidationException is often thrown when a validation error occurs by some process within an applications request life-cycle.
Illuminate\Contracts\Validation\ Validation The Validator interface defines the methods and behaviors of the frameworks validator component. The Validator component actually extends the MessageProvider interface.
Illuminate\Http\RedirectResponse The RedirectReponse class is responsible for setting the correct headers required to redirect a user's browser to another URL.
Illuminate\Support\MessageBag The MessageBag class is the default implementation of the Illuminate\Contracts\Support\MessageBag interface. It also implements the MessageProvider interface to return a reference to itself.
Illuminate\View\View The View class uses instances of MessageProvider implementations to facilitate the communication of errors between the application logic and the front-end code of an application.

#Continue Reading

This article is the start of a mini-series about Laravel's ErrorMessageBag component. Click through the rest of the articles to continue reading:

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.