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

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
    }
}
  • 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 – Daniel Lavric Nov 3 '17 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);
    }
  • 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. – Daniel Lavric Nov 3 '17 at 16:06
  • Can you please add your Model file? – Abhishek Panchal Nov 3 '17 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); } } – Daniel Lavric Nov 3 '17 at 16:16
  • $this->_init('DD\Reservations\Model\ResourceModel\QuoteReser‌​vation'); please change this line and try again – Abhishek Panchal Nov 3 '17 at 16:22

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.