2

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.phplooks 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.

7ochem
7,61516 gold badges54 silver badges82 bronze badges
asked Feb 26, 2017 at 13:03
7
  • Please be more specific with 500 error. Look for some details in magento logs or server php logs Commented Feb 26, 2017 at 14:01
  • 1
    How about your issue? Commented Feb 27, 2017 at 17:15
  • @KhoaTruongDinh I edited my question and added the errors I get. Commented Feb 27, 2017 at 19:25
  • You should check the arguments passed to the construct method. Which is your application mode? You should read more here: github.com/magento/magento2/issues/4896. Can you post your app/code/Plazathemes/Layerednavigation/Controller/Category/View.php? Commented Feb 27, 2017 at 23:02
  • @KhoaTruongDinh Magento is currently in production mode. I checked the issue there and it is very similar, but I cannot find how to adapt my own code. I added the complete view.php file to my question. Commented Feb 28, 2017 at 20:14

2 Answers 2

2

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 and global.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.

answered Mar 3, 2017 at 10:04
1
  • Is this better way then why we need to run di compile Commented Apr 11, 2018 at 6:37
0

1) You should check your current application mode:

php bin/magento deploy:mode:show

If the current mode is developer mode. You shouldn't run di compile. Di compile is used for production mode.

2) 500 Error is a server error. You should enable Display Errors in app/bootstrap.php or index.php file to see the error.

ini_set('display_errors', -1); // Remember to set -1

NOTE: should enable cache and check the permission properly.

answered Feb 26, 2017 at 15:25
1
  • I edited my question and added the errors I get. Commented Feb 27, 2017 at 19:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.