I am working on a Magento 2.1 website for one of my clients. When running setup:di:compile
, my Magento files are successfully compiled without errors.
When I open the frontend of my website, my homepages and other CMS pages work fine. But when I go to a category page, like /shop
, I get a HTTP error 500.
The strange thing is, once I delete the var/di folder, this error disappears and I can visit all my pages and categories, but my website becomes extremely slow (like 11 seconds to load a simple page).
So what could cause the HTTP 500 error after compiling and what could be wrong with my var/di folder?
EDIT: after turning on PHP error reporting, the following error is shown when visiting category pages (not homepage and CMS pages):
Fatal error: Uncaught TypeError: Argument 1 passed to
Plazathemes\LayeredNavigation\Controller\Category\View::__construct()
must be an instance of Magento\Framework\App\Action\Context, instance
of Magento\Framework\ObjectManager\ObjectManager given, called in
vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php
on line 93 and defined in
app/code/Plazathemes/Layerednavigation/Controller/Category/View.php:27
Stack trace: #0
vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93):
Plazathemes\LayeredNavigation\Controller\Category\View->__construct(Object(Magento\Framework\ObjectManager\ObjectManager))
#1 vendor/magento/framework/ObjectManager/Factory/Compiled.php(88):
Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Plazathemes\Lay…',
Array)
#2
vendor/magento/framework/ObjectManager/ObjectManager.php(57):
Magento\F in
app/code/Plazathemes/Layerednavigation/Controller/Category/View.php
on line 27
The file app/code/Plazathemes/Layerednavigation/Controller/Category/View.php
looks like this:
namespace Plazathemes\LayeredNavigation\Controller\Category;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Result\PageFactory;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class View extends \Magento\Catalog\Controller\Category\View
{
/**
* Catalog Layer Resolver
*
* @var Resolver
*/
private $layerResolver;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Catalog\Model\Design $catalogDesign,
\Magento\Catalog\Model\Session $catalogSession,
\Magento\Framework\Registry $coreRegistry,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository
)
{
parent::__construct($context, $catalogDesign, $catalogSession, $coreRegistry, $storeManager, $categoryUrlPathGenerator, $resultPageFactory, $resultForwardFactory, $layerResolver, $categoryRepository);
$this->layerResolver = $layerResolver;
}
public function execute()
{
$layer_action = $this->getRequest()->getParam('layer_action');
if($layer_action == 1) {
if ($this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)) {
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl());
}
$category = $this->_initCategory();
if ($category) {
$this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY);
$settings = $this->_catalogDesign->getDesignSettings($category);
if ($settings->getCustomDesign()) {
$this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
}
$this->_catalogSession->setLastViewedCategoryId($category->getId());
$page = $this->resultPageFactory->create();
if ($settings->getPageLayout()) {
$page->getConfig()->setPageLayout($settings->getPageLayout());
}
if ($category->getIsAnchor()) {
$type = $category->hasChildren() ? 'layered' : 'layered_without_children';
} else {
$type = $category->hasChildren() ? 'default' : 'default_without_children';
}
if (!$category->hasChildren()) {
$parentType = strtok($type, '_');
$page->addPageLayoutHandles(['type' => $parentType]);
}
$page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]);
$layoutUpdates = $settings->getLayoutUpdates();
if ($layoutUpdates && is_array($layoutUpdates)) {
foreach ($layoutUpdates as $layoutUpdate) {
$page->addUpdate($layoutUpdate);
}
}
$product_list = $page->getLayout()->getBlock('category.products.list')->toHtml();
if($page->getLayout()->getBlock('catalog.leftnav')) {
$leftLayer = $page->getLayout()->getBlock('catalog.leftnav')->toHtml();
} else {
$leftLayer = false;
}
$data['leftLayer'] = $leftLayer;
$data['productlist'] = $product_list;
$this->getResponse()->representJson(
$this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($data)
);
} elseif (!$this->getResponse()->isRedirect()) {
return $this->resultForwardFactory->create()->forward('noroute');
}
} else {
return parent::execute();
}
}
}
I have found this issue on GitHub (https://github.com/magento/magento2/issues/4896) which is very similar to mine, but I cannot find how to adapt my code.
Best Answer
Just in case someone else comes across this, I finally got it to work.
There seemed to be a problem with two files inside the
var/di
folder:frontend.ser
andglobal.ser
. Once these files were deleted, everything works just fine and the internal server error disappears.I am still not sure what caused these files to become a problem, but at least just deleting them got me going again and fixed the issue.
Hopefully this solution will work for others experiencing this same problem.