2

I am working through Alan Storm's tutorial about the first steps in setting up a Controller in Magento 2 and came across an odd little error on page load after setting up my route:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/magento_blog/app/code/{Vendor}/{Module}/Controller/Hello/World.php:26)

where line 26 is the var_dump in the controller file:

class World extends \Magento\Framework\App\Action\Action
{
    protected $pageFactory;

    public function __construct(Context $context, PageFactory $pageFactory)
    {
        $this->pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        var_dump(__METHOD__);
        $page_object = $this->pageFactory->create();
        return $page_object;
    }
}

So the "fix" is to comment this var_dump out and the file works without errors, but that's not what i'm interested in. This kind of error is very rare in Magento, as the rendering is really out of our hands. As long as the configs are right, the page will render.

The common reason for this is a UTF-8 error or some white space at the beginning of the PHP file, both of which aren't happening from my end, as i'm simply calling a controller and dumping the method name.

I have made sure to clear the cache and generation folders, and when i do and reload the page, the page factory does regenerate just fine. I'm not sure if this is where my error might be coming from, as i'm not too accustom to looking over these files (they are generated right???).

If i comment out the lines:

$page_object = $this->pageFactory->create();
return $page_object;

The var_dump will display on the constructed url of the controller. So, in a sense, the issue is is when the $page_object is outputted.

And ideas what might be causing this?

5
  • 1
    Maybe try to remove the return inside your constructor, just call the parent and instead using var_dump prefer to use Magento 2 log system magento.stackexchange.com/questions/92434/… Commented Feb 26, 2017 at 9:01
  • Good call on the return in the constructor. You idea definitely works, it outputs the __METHOD__ to the debug.log. So in concept, it's a work-around. But what i would like to understand is why the error is kicking in the first place. Also, there is nothing like this on Stacks that i can find, so wanted to get the issue out to the community.
    – circlesix
    Commented Feb 26, 2017 at 16:06
  • 1
    You get that headers message because you are printing something before rendering page, it's normal. Why do you want to print something this way? Can you please explain what do you need? Commented Feb 26, 2017 at 18:20
  • I'm just working through the tutorial, and this is how it's called out to structure the code. The more i play with it, the more that all makes sense. There is no real world application for this, it's just playing around with the system and seeing how it works. But you are right, i don't this there is a basic fault here, just playing with thing till they break. Do you want to post that last comment as an answer to i can accept it?
    – circlesix
    Commented Feb 26, 2017 at 19:00
  • 1
    Ok :) enjoy M2 and welcome! Thanks in advance for your approval! Commented Feb 26, 2017 at 19:03

2 Answers 2

1

Remove the return inside your constructor, just call the parent and instead using var_dump prefer to use Magento 2 log system magento.stackexchange.com/questions/92434/

You get headers already sent by message because you are printing something before rendering page.

0

This happend to me because I accidentially added a empty line before the <?php opening tag.

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.