0

Notice: Undefined variable: modelRepository in /home/server/web/server.dev.vendor.com/public_html/app/code/TemplateMonster/FilmSlider/Block/Widget/FilmSlider.php on line 121

public function createSlider()
{
    try {
        $sliderId = $this->getSliderId();
        $modelRepository = $this->_filmSliderRepository->getById($sliderId);
    } catch (\Exception $e) {
        echo __('Can not load slider');
    }
    return $modelRepository;
}

Just updated to 2.3.0 and get this error when open home page, how can i solve this?

0
2

This error comes because $modelRepository variable you have return but some how your try block not execute.

Initialise variable $modelRepository with blank before try block like

$modelRepository = '';

Please let me know still you are getting error.

0

Your following code snippet throwing an error

try {
    $sliderId = $this->getSliderId();
    $modelRepository = $this->_filmSliderRepository->getById($sliderId);
} catch (\Exception $e) {
    echo __('Can not load slider');
}

That's why $modelRepository is null.

You can try the following way for fixing this error:

public function createSlider()
{
    try {
        $sliderId = $this->getSliderId();
        $modelRepository = $this->_filmSliderRepository->getById($sliderId);
    } catch (\Exception $e) {
        $modelRepository = $this->_filmSlideFactory->create();
        echo __('Can not load slider');
    }
    return $modelRepository;
}

Where $this->_filmSlideFactory is the instance of FilmSlider model.

0

Inside echo echo __('Can not load slider'); directly from is not good.As per as Magento standard.

You have to return this some value from catch block instead of echo,

public function createSlider()
{
    try {
        $sliderId = $this->getSliderId();
        $modelRepository = $this->_filmSliderRepository->getById($sliderId);
    } catch (\Exception $e) {
     //return from here
        return  __('Can not load slider');
    }
    return $modelRepository;
}

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.