Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Laravel News:
Introducing View Components in Laravel, an alternative to View Composers
May 16, 2018 @ 15:55:56

On the Laravel News site there's a new post covering a refactoring of view handling that's possible with recent versions of the framework: using view components instead of view composers..

In software development, one of the “best practices” is to create reusable code that can be implemented in different parts of your application if needed. [...] View composers allow you to move the logic outside your controller and pass the data to the specified set of views. [Using view components instead of composers lets you] reuse complex components using dynamic data on any view within your application.

To help illustrate the difference they set up a scenario of a blog with a "highlights" sidebar based on data from an API response. With view composers you could extract this logic out of the controllers and add it more automatically to the view itself. They point out that this can work for a majority of the situations there's another method that is even more flexible: a reusable component implemented directly on the view. He provides the complete code showing an examples of this components, including a custom Blade directive.

tagged: laravel tutorial view composer component refactor

Link: https://laravel-news.com/introducing-view-components-on-laravel-an-alternative-to-view-composers


Trending Topics: