<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sam Jones</title>
    <description>The latest articles on Forem by Sam Jones (@slashequip).</description>
    <link>https://forem.com/slashequip</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F238775%2F90388762-8227-46ef-9811-31be885002ec.jpeg</url>
      <title>Forem: Sam Jones</title>
      <link>https://forem.com/slashequip</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/slashequip"/>
    <language>en</language>
    <item>
      <title>Taking a class-based approach to views in Laravel.</title>
      <dc:creator>Sam Jones</dc:creator>
      <pubDate>Sun, 25 Nov 2018 12:00:00 +0000</pubDate>
      <link>https://forem.com/slashequip/taking-a-class-based-approach-to-views-in-laravel-3lmi</link>
      <guid>https://forem.com/slashequip/taking-a-class-based-approach-to-views-in-laravel-3lmi</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Note. this article was originally published on Medium on Mar 15, 2018.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I’ve recently been working on a simple Laravel package called &lt;a href="https://github.com/haganjones/laravel-viewables" rel="noopener noreferrer"&gt;Laravel Viewables&lt;/a&gt; that wraps up my approach to class-based views in Laravel. I use it in pretty much all the medium/large sized projects I work on.&lt;/p&gt;

&lt;p&gt;For a while now I’ve wanted to write down my thoughts on why I take this approach and how others can benefit from it. Imposter syndrome has prevented me from publishing this sooner but I got there in the end! HI-FIVE to my fellow imposters!&lt;/p&gt;

&lt;h3&gt;
  
  
  When To Use Class-Based Views?
&lt;/h3&gt;

&lt;p&gt;To be clear here unless &lt;em&gt;you know&lt;/em&gt; you are going to need to extract views into classes then I don’t think a project should ever be started using this approach from the get-go. Class-based views can be implemented very simply during a refactor, when controller methods start to become bloated with dependancies that are specific to the view itself.&lt;/p&gt;

&lt;p&gt;A great example of this would be an ecommerce application report page that involves sales revenue aggregation. Think about the separation of concerns; should the controller know (or care) about any of these things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The name and location of the view file.&lt;/li&gt;
&lt;li&gt;How specific view data is retrieved/calculated and…&lt;/li&gt;
&lt;li&gt;…the names of variables the view is expecting to receive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Personally, no, I don’t think so. It would be much more convenient if all this view logic was kept in it’s own class with it’s own dependancies, this is where class-based views can help clean up controllers and help manage complex views more easily.&lt;/p&gt;

&lt;p&gt;Using class-based views reduces how tightly coupled the controller is to a specific view/&lt;a href="https://laravel.com/docs/5.6/blade" rel="noopener noreferrer"&gt;blade&lt;/a&gt; file and shifts that coupling to a dedicated class. This reduction in coupling becomes more apparent when controllers have multiple methods that return views.&lt;/p&gt;

&lt;p&gt;Again, to be clear, I’m not suggesting turning all views in class-based views, don’t over or pre optimised code for simple views with simple logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  I’m Sold. How Do They Work?
&lt;/h3&gt;

&lt;p&gt;Starting with Laravel 5.5 the framework shipped with a Responsable interface, implementing this interface required including a toResponse() method on the Responsable class. A new instance of that class can now be returned directly from a controller.&lt;/p&gt;

&lt;p&gt;Adam Wathan put up a nice little example of a Responsable class on Twitter:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-897836363885797376-8" src="https://platform.twitter.com/embed/Tweet.html?id=897836363885797376"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-897836363885797376-8');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=897836363885797376&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;h3&gt;
  
  
  Ok, So What Are Viewables?
&lt;/h3&gt;

&lt;p&gt;Viewables (as I named them) are built on top of the Responsable interface and build in functionality and methods similar to how Laravel’s &lt;a href="https://laravel.com/docs/5.6/mail#generating-mailables" rel="noopener noreferrer"&gt;Mailable&lt;/a&gt; classes work. They provide a more fluent way to build out class-based views.&lt;/p&gt;

&lt;p&gt;The full documentation can be found &lt;a href="https://github.com/haganjones/laravel-viewables/blob/master/README.md" rel="noopener noreferrer"&gt;here&lt;/a&gt; but I’ll re-refactor the above example from &lt;a href="https://adamwathan.me/" rel="noopener noreferrer"&gt;Adam&lt;/a&gt;’s example and sprinkle in some comments to show how it can be done.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;These examples are fairly basic and overly simplified but I hope you can see how the use of class-based views can help clean up controllers and separate complex view logic into more manageable classes.&lt;/p&gt;

&lt;p&gt;Let me know if this post has helped you or if you have any questions I’d be glad to try and clear things up.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>blade</category>
    </item>
  </channel>
</rss>
