1

I have created a custom module and I want to inject my model in a constructor , but when I inject the model class then my controller stops working.

namespace DD\Reservations\Controller\Url;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\Action;
use DD\Reservations\Model\QuoteReservation;
use Psr\Log\LoggerInterface;

class TestResponse extends Action{

    private $logger;

    public function __construct(
        Context $context,
       // QuoteReservation $quoteReservation,
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;
        parent::__construct($context);
    }

    public function execute()
    {
        echo 'This is from test response';
        $this->logger->debug('This is debug from test response');
    }
}

If i uncomment the commented line from constructor my controller won't display the echo message anymore , neither the log message.

This is my model file:

<?php

namespace DD\Reservations\Model;

use Magento\Framework\Model\AbstractModel;

class QuoteReservation extends AbstractModel{

    protected function _construct()
    {
        $this->_init(\DD\Reservations\Model\ResourceModel\QuoteReservation::class);
    }
}

2 Answers 2

2

First of all you must flush and clear your cache after injecting any class in your constructor. You can upgrade your setup if flush cache does not work as expected. So run below command:

php bin/magento cache:flush
php bin/magento cache:clean
php bin/magento setup:upgrade

and give write permission to var and pub if they have not.

You can use below code and let me know if still face any problem.

namespace DD\Reservations\Controller\Url;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\Action;
use DD\Reservations\Model\QuoteReservation;
use Psr\Log\LoggerInterface;

class TestResponse extends Action{

    private $logger;
    protected $quoteReservation;

    public function __construct(
        Context $context,
        QuoteReservation $quoteReservation,
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;
        $this->quoteReservation = $quoteReservation;
        parent::__construct($context);
    }

    public function execute()
    {
        echo 'This is from test response';
        $this->logger->debug('This is debug from test response');
        $quoteReservation = $this->quoteReservation->create();
        //use as you wish
    }
}
1
  • Thanks it worked not the create method cause it's not a factory but it compiled and everything. I was running only cache:flush i thought it was enough. Thanks again Nov 3, 2017 at 20:08
0

You have to declare $quoteReservation as a variable. just after $logger

protected $quoteReservation;

and then

public function __construct(
        Context $context,
        QuoteReservation $quoteReservation,
        LoggerInterface $logger
    )
    {
        $this->logger = $logger;
        $this->quoteReservation= $quoteReservation
        parent::__construct($context);
    }
4
  • No that is not the problem. Didn't work. I thought that i can't use models inside controllers but core controllers from magento 2 use them. Nov 3, 2017 at 16:06
  • Can you please add your Model file? Nov 3, 2017 at 16:15
  • <?php namespace DD\Reservations\Model; use Magento\Framework\Model\AbstractModel; class QuoteReservation extends AbstractModel{ protected function _construct() { $this->_init(\DD\Reservations\Model\ResourceModel\QuoteReservation::class); } } Nov 3, 2017 at 16:16
  • $this->_init('DD\Reservations\Model\ResourceModel\QuoteReser‌​vation'); please change this line and try again Nov 3, 2017 at 16:22

Your Answer

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

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