{"id":194932,"date":"2019-08-31T17:05:29","date_gmt":"2019-08-31T17:05:29","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=194932"},"modified":"2023-01-05T11:09:44","modified_gmt":"2023-01-05T11:09:44","slug":"viewmodel-and-srp-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/","title":{"rendered":"ViewModel and SRP  in Magento 2"},"content":{"rendered":"\n<p>Before studying ViewModel feature of magento, let&#8217;s understand first SRP. <br><strong>SRP<\/strong> stands for <strong>S<\/strong>ingle <strong>R<\/strong>esponsibility <strong>P<\/strong>rinciple : Every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.<br>In simple words : Each class should ideally have only one purpose to change.<br>Basically this is a part of <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/SOLID\">SOLID<\/a><\/strong> Principle of Software Development.<br><br><strong>VIEW MODEL<\/strong> :<br>View model is class that we inject to phtml, so that require and related data could be access from it.<br>View Model helps us to avoid unnecessary overriding of the block class Magento\\Framework\\View\\Element\\Template &#8216;s  constructor .<br><br><strong>View Model Implementation<\/strong> :<br>To implement the view model, we need to add the argument in block arguments with xsi type object  as <strong>xsi:type=&#8221;object&#8221;<\/strong><br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;block name=&quot;custom.block&quot; template=&quot;Vendor_Module::custom.phtml&quot;&gt;\n    &lt;arguments&gt;\n        &lt;argument name=&quot;view_model&quot; xsi:type=&quot;object&quot;&gt;&lt;Vendor&gt;\\&lt;Module&gt;\\ViewModel\\Custom&lt;\/argument&gt;\n    &lt;\/arguments&gt;\n&lt;\/block&gt;<\/pre>\n\n\n\n<p>To create the view model we need to implement the Argument Interface.<\/p>\n\n\n\n<p><code> \\Magento\\Framework\\View\\Element\\Block\\ArgumentInterface;<\/code><br><\/p>\n\n\n\n<p>Now let&#8217;s create a ViewModel,<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Webkul Software.\n *\n * PHP version 7.0+\n *\n * @category  Webkul\n * @package   Webkul_Test\n * @author    Webkul &lt;support@webkul.com&gt;\n * @copyright Webkul Software Private Limited (https:\/\/webkul.com)\n * @license   https:\/\/store.webkul.com\/license.html ASL Licence\n * @link      https:\/\/store.webkul.com\/license.html\n *\/\nnamespace &lt;Vendor&gt;\\&lt;Module&gt;\\ViewModel;\n\nuse Magento\\Framework\\View\\Element\\Block\\ArgumentInterface;\n\nclass Custom implements ArgumentInterface\n{\n\/\/ Do some stuff here.\n}<\/pre>\n\n\n\n<p>To get the View model in phtml file, we just need to call the name of the argument via block reference as follows<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/** @var \\Magento\\Framework\\View\\Element\\Template $block*\/\n$block-&gt;getData(&quot;view_model&quot;);\n\/\/OR\n$block-&gt;getViewModel();<\/pre>\n\n\n\n<p><strong>Conclusion <\/strong>:<br>With the help of viewModel , it benefits us that we don&#8217;t need to create the block class and inject the unnecessary Injection of the Dependencies and call parent.<br>Most importantly it help us to implement SOLID Property SRP.<br><br>That all for now, Happy coding.!!!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before studying ViewModel feature of magento, let&#8217;s understand first SRP. SRP stands for Single Responsibility Principle : Every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.In simple words : Each class should ideally have only one purpose to <a href=\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":170,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2056,2070,9716,9713,9875,9715],"class_list":["post-194932","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-magento","tag-magento2","tag-solid-principle","tag-srp","tag-view-model","tag-viewmodel"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>ViewModel and SRP in Magento 2 - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ViewModel and SRP in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Before studying ViewModel feature of magento, let&#8217;s understand first SRP. SRP stands for Single Responsibility Principle : Every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.In simple words : Each class should ideally have only one purpose to [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-31T17:05:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-05T11:09:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prabhat Rawat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prabhat Rawat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\"},\"author\":{\"name\":\"Prabhat Rawat\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f\"},\"headline\":\"ViewModel and SRP in Magento 2\",\"datePublished\":\"2019-08-31T17:05:29+00:00\",\"dateModified\":\"2023-01-05T11:09:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\"},\"wordCount\":222,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"magento\",\"Magento2\",\"Solid principle\",\"SRP\",\"view model\",\"viewmodel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\",\"name\":\"ViewModel and SRP in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2019-08-31T17:05:29+00:00\",\"dateModified\":\"2023-01-05T11:09:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ViewModel and SRP in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f\",\"name\":\"Prabhat Rawat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prabhat Rawat\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/prabhat-rawat763\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ViewModel and SRP in Magento 2 - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"ViewModel and SRP in Magento 2 - Webkul Blog","og_description":"Before studying ViewModel feature of magento, let&#8217;s understand first SRP. SRP stands for Single Responsibility Principle : Every class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.In simple words : Each class should ideally have only one purpose to [...]","og_url":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2019-08-31T17:05:29+00:00","article_modified_time":"2023-01-05T11:09:44+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Prabhat Rawat","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prabhat Rawat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/"},"author":{"name":"Prabhat Rawat","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f"},"headline":"ViewModel and SRP in Magento 2","datePublished":"2019-08-31T17:05:29+00:00","dateModified":"2023-01-05T11:09:44+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/"},"wordCount":222,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["magento","Magento2","Solid principle","SRP","view model","viewmodel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/","name":"ViewModel and SRP in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2019-08-31T17:05:29+00:00","dateModified":"2023-01-05T11:09:44+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/viewmodel-and-srp-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"ViewModel and SRP in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/3d52d6c1ad8a809d2f7548e6a9c3358f","name":"Prabhat Rawat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/616ec5deaf63b72b3003cf99e608ae354f4d373cee8d25b2d0bfa65cab270ad8?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prabhat Rawat"},"url":"https:\/\/webkul.com\/blog\/author\/prabhat-rawat763\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/194932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/170"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=194932"}],"version-history":[{"count":8,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/194932\/revisions"}],"predecessor-version":[{"id":363247,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/194932\/revisions\/363247"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=194932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=194932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=194932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}