No pain, no gain.
It’s not uncommon to have tens, if not hundreds of views in a Laravel application. Something that soon gets out of hand is the various references to routes. Think about how many times you’ve done something like this in your blade views:
If for whatever reason we have to make a change to either the route alias or default query string values you’ll soon find yourself doing mass string replacements across your entire application which brings the risk of breakage within many files.
What can we do to possibly better handle this? There are a couple of different approaches.
Then in your views it would look like this:
Super clean, right? For you advanced developers you’ll probably want to go with this next approach.
At first glance, you’ll see some similarities, but the key difference is that our accessor will return an instance of the presenter.
Then you would use it like this:
As you can see with either approach, the view now doesn’t care about how we determine the URL, just that a URL is returned. The beauty to this is should you have to adjust any routes you only have to edit two files, not hundreds.