3

This is my block class for form

Smartshore/Subscription/Block/SubscriptionForm

namespace Smartshore\Subscription\Block;
class SubscriptionForm extends \Magento\Framework\View\Element\Template
{
 protected $_subscriptionLoader; 
 public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Catalog\Model\SubscriptionFactory $_subscriptionLoader
 ) {
 $this->_subscriptionLoader = $_subscriptionLoader;
 parent::__construct($context);
 }
 public function getSubscriptionBy($id)
 {
 return $this->_subscriptionLoader->create()->load($id);
 }
}

Smartshore/Subscription/view/frontend/layout/subscription_index_add.xml

This is my xml file

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <head>
 <title>New Subscription</title>
 </head>
 <body>
 <referenceContainer name="content">
 <block class="Smartshore\Subscription\Block\SubscriptionForm" name="subscriptionform.add" template="Smartshore_Subscription::form.phtml"/>
 </referenceContainer>
 </body>
</page>

In my template(Smartshore/Subscription/view/frontend/templates/form.phtml) I'm using $this->getSubscriptionBy($id);

But I'm getting two errors:

Invalid block type: Smartshore\Subscription\Block\SubscriptionForm
Class Magento\Catalog\Model\SubscriptionFactory does not exist

What is causing the errors here?

Jackson
9,98933 gold badges132 silver badges217 bronze badges
asked Dec 8, 2016 at 8:11
4
  • Please give with full File Path from Magento 2 & Which files u have created Commented Dec 8, 2016 at 8:23
  • Please see my edited question @AnkitShah Commented Dec 8, 2016 at 8:28
  • Please check ur etc folder's module.xml & di.xml if u have. Find in whole folder Magento\Catalog Commented Dec 8, 2016 at 8:33
  • have you created Subscription model if yes then post model code and path of model Commented Dec 8, 2016 at 8:36

2 Answers 2

1

Error comes from the __construct method in your block (di):

public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Catalog\Model\SubscriptionFactory $_subscriptionLoader
) {
// ....

There is no class \Magento\Catalog\Model\SubscriptionFactory in the Magento. You should check what model you are trying to use in your block. Possible, it should be your own model (the corresponding model of your module)?

answered Dec 8, 2016 at 9:20
1

I got the problem solved. Please refer to the block class, I was having the problem of factory Class path

Smartshore/Subscription/Block/SubscriptionForm

<?php
namespace Smartshore\Subscription\Block;
class SubscriptionForm extends \Magento\Framework\View\Element\Template
{
 /**
 * @var \Smartshore\Subscription\Model\ResourceModel\Subscription\CollectionFactory
 */
 protected $_subscriptionCollectionFactory;
 /**
 * Construct
 *
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Smartshore\Subscription\Model\ResourceModel\Subscription\CollectionFactory $subscriptionCollectionFactory,
 * @param array $data
 */
 public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Smartshore\Subscription\Model\SubscriptionFactory $_subscriptionLoader,
 array $data = []
 ) {
 parent::__construct($context, $data);
 $this->_subscriptionLoader = $_subscriptionLoader;
 }
 public function getSubscriptionById($id)
 {
 return $this->_subscriptionLoader->create()->load($id);
 }
}
answered Dec 9, 2016 at 7:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.