0

I am trying to get data from a database table, my code of Index.php

<?php
namespace Demo\Helloworld\Controller\Post;
class Index extends \Magento\Framework\App\Action\Action
{
    protected $_postFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Pixelpro\Helloworld\Model\PostFactory $postFactory
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->_postFactory = $postFactory;
        return parent::__construct($context);
    }
    public function execute()
    {
        
        $post = $this->_postFactory->create();
        $collection = $post->getCollection();
        foreach($collection as $item){
            echo "<pre>";
            print_r($item->getData());
            echo "</pre>";
        }
        
        
    }
}

I tried to run these commands but the error still appears

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

the error :

1 exception(s): Exception #0 (ReflectionException): Class Demo\Helloworld\Controller\Post\index\Interceptor does not exist Exception #0 (ReflectionException): Class Demo\Helloworld\Controller\Post\index\Interceptor does not exist#1 Magento\Framework\Code\Reader\ClassReader->getConstructor() called at [vendor/magento/framework/ObjectManager/Definition/Runtime.php:54]

2
  • Try running php bin/magento setup:di:compile if you are not in developper mode. Also make sure that your class path reflect your folder path. Your file should be located there : app/code/Demo/Helloworld/Controller/Post/Index.php. your file name should also be Index.php and not index.php Commented Dec 14, 2021 at 21:54
  • I renamed it to Index.php instead of index.php but the error still available, and I am on the developer mode.
    – MMM
    Commented Dec 18, 2021 at 14:34

1 Answer 1

0

just remove generated folder and hit below cmd. make sure your controller name is correct Index.php.

rm -rf generated/* var/*;
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
1
  • I executed these commands but the error still available, and the last command I got this : Deprecated Functionality: Array and string offset access syntax with curly braces is deprecated in /home/626252.cloudwaysapps.com/cgxbwegvut/public_ht ml/app/code/Magefan/Blog/Model/Import/Wordpress.php on line 125
    – MMM
    Commented Dec 18, 2021 at 14:38

Your Answer

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

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