1

I have created a custom module: Ashore_Faq

My module structure is:

Ashore
└───Faq
    │   registration.php
    │
    ├───etc
    │       module.xml
    │
    ├───Model
    │   │   Custom.php

    │   │
    │   └───ResourceModel
    │       │   Custom.php
    │       │
    │       └───Custom
    │               Collection.php
    │
    ├───Setup
    │       InstallSchema.php
    │       
    │
    └───view
        └───frontend
            │___Template
            │       Faq
                        Index.php
            ├───layout
            │       faq_faq_index.xml
            │

After creation of module, I have written below code but it's not working.

Below is code with folder structure:

/block/Faq/Index.php

<?php

namespace Ashore\Faq\Block\Faq;
use Ashore\Faq\Block\BaseBlock;

class Index extends BaseBlock
{
    //public $hello='Hello World';

    protected $mymodulemodelFactory;
    public function __construct(
    \Ashore\Faq\Model\ResourceModel\Custom\CollectionFactory $mymodulemodelFactory, 
    )
    {
        $this->mymodulemodelFactory = $mymodulemodelFactory;
    }
    public function getCollection()
    {
        $collection = $this->mymodulemodelFactory->create();
        return $collection;
    }


}

AND

get collection data folder structure:

/View/Frontend/Template Faq.Index.php

echo $block->getCollection();

but it's not working. After running the code it displays the blank page.

Please suggest a solution?

  • plz let me know if you have issue – Rakesh Jesadiya Jun 23 '17 at 5:52
  • just set, $collection = $this->mymodulemodelFactory->create()->getCollection() in your Block file; – Rakesh Jesadiya Jun 23 '17 at 5:58
  • please show your BaseBlock.php file code – Rakesh Jesadiya Jun 23 '17 at 6:25
1
<?php

namespace Ashore\Faq\Block\Faq;

use Magento\Framework\App\RequestInterface;
use Ashore\Faq\Block\BaseBlock;
use Ashore\Faq\Model\ResourceModel\Custom\CollectionFactory;

class Index extends \Magento\Framework\View\Element\Template 
{
    protected $mymodulemodelFactory;
    protected $request;

    public function __construct(
    CollectionFactory $mymodulemodelFactory,
    \Magento\Catalog\Block\Product\Context $context,
    RequestInterface $request,
    array $data = [] 
    )
    {
        parent::__construct($context, $data);
        $this->_request = $request;
        $this->mymodulemodelFactory = $mymodulemodelFactory;
    }
    public function getCollection()
    {
        $collection = $this->mymodulemodelFactory->getCollection();
        return $collection;
    }
}

Hey @Devidas I hope It's Work For you

-1

You can get data using foreach of your collection,

class Index extends BaseBlock
{
    //public $hello='Hello World';

     protected $mymodulemodelFactory;
     public function __construct(
       \Ashore\Faq\Block\Context $context,
       \Ashore\Faq\Model\CustomFactory $mymodulemodelFactory
     )
     {
        $this->mymodulemodelFactory = $mymodulemodelFactory;
        parent::__construct($context);
     }
    public function getCollection()
    {
        $collection = $this->mymodulemodelFactory->create()->getCollection()->addAttributeToSelect('*');
        return $collection;
    }
}

in Block file,

$collection = $block->getCollection();
foreach($collection as $moduledata){
    echo "<pre>";print_r($moduledata->getData());
}

Remove var folder and check.

  • Not working ...dislpay blank page – Devidas Jun 23 '17 at 5:57
  • plz check Block file its correct or not – Devidas Jun 23 '17 at 5:58
  • not working -display blank page ....plz give me different solution to collection of module data – Devidas Jun 23 '17 at 6:05
  • i have set, $collection = $this->mymodulemodelFactory->create()->getCollection() in my Block file but its not working – Devidas Jun 23 '17 at 6:07
  • 1
    Don't place model/helper/resourcemodel functions in controller files – treyBake Nov 24 '17 at 15:12
-2

BaseBlock Code

 /**
 * @var \Magento\Framework\Url
 */
 protected $_urlApp;

 /**
 * @var \Ashore\Faq\Model\Config
 */
protected $_config;

/**
 * @param \Ashore\Faq\Block\Context $context
 * @param \Magento\Framework\UrlFactory $urlFactory
 */
public function __construct( \Ashore\Faq\Block\Context $context
)
{
    $this->_devToolHelper = $context->getFaqHelper();
    $this->_config = $context->getConfig();
    $this->_urlApp=$context->getUrlFactory()->create();
    parent::__construct($context);

}

/**
 * Function for getting event details
 * @return array
 */
public function getEventDetails()
{
    return  $this->_devToolHelper->getEventDetails();
}

/**
 * Function for getting current url
 * @return string
 */
public function getCurrentUrl(){
    return $this->_urlApp->getCurrentUrl();
}

/**
 * Function for getting controller url for given router path
 * @param string $routePath
 * @return string
 */
public function getControllerUrl($routePath){

    return $this->_urlApp->getUrl($routePath);
}

/**
 * Function for getting current url
 * @param string $path
 * @return string
 */
public function getConfigValue($path){
    return $this->_config->getCurrentStoreConfigValue($path);
}

/**
 * Function canShowFaq
 * @return bool
 */
public function canShowFaq(){
    $isEnabled=$this->getConfigValue('faq/module/is_enabled');
    if($isEnabled)
    {
        $allowedIps=$this->getConfigValue('faq/module/allowed_ip');
         if(is_null($allowedIps)){
            return true;
        }
        else {
            $remoteIp=$_SERVER['REMOTE_ADDR'];
            if (strpos($allowedIps,$remoteIp) !== false) {
                return true;
            }
        }
    }
    return false;
}

 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.