Why nette always show default 500 error instead of 404 error template?

4 years ago

silviustan
Member | 17
+
0
-

Hi,

I have an error presenter like here: "":https://github.com/…resenter.php

In bootstrap.php I have this line:

<?php
$container->getService('application')->errorPresenter = 'Front:Error';
?>

Error presenter is located in FrontModule and I have Latte templates for every kind of error: 404, etc.

My problem is that, when a 404 error is thrown, the Nette default 500 error template is shown (of course, in production mode).

Can anybody tell me what am I doing wrong? :)

Thanks in advance!

4 years ago

Oli
Member | 1219
+
0
-

I would say, that problem can be in your Error presenter. While you process error message (eg. 404), Nette throw some exception and return 500. Try check your log folder, there should be some file(s) with more information.

Almost every time when it happend to me, the problem was with it.

btw. Have your presenter right namespace with FrontModule?

4 years ago

silviustan
Member | 17
+
0
-

Hi Oli,

Thanks for your reply. This will help me cause I found the problem in logs. A fatal error stops the execution until the error presenter is loading. I'm a beginner so, from now on I know what should I do.

Yes, error presenter has the right namespace.

Thank you! Problem will be solved!

4 years ago

silviustan
Member | 17
+
0
-

May I disturb you again with a question?

My error comes from a function that gets from database some categories to build the menu of the website. This function is called in beforeRender() of the BasePresenter (all other presenters extend this presenter so I thought is the best way to do it here because will run for all pages).

Can you recommend me where to collect common data (as menu items) that are used among the all pages?

The fatal error was: “Call to a member function table() on a non-object” but is strange that the function that build menu works well when a route is matching the rules.

4 years ago

Oli
Member | 1219
+
0
-

Can you give here how you inject database to BasePresenter and how you give menu to template? Have you in Your BasePresenter something like this? Or you use constructor injection?

/** @var \Nette\Database\Context @inject */
public $context;

protected function beforeRender()
{
    $this->template->menu = $this->context->table('menu')->where(/* ... */);
    parent::beforeRender();
}

4 years ago

silviustan
Member | 17
+
0
-

I'm using constructor injection.

So, in BasePresenter.php:

<?php
public $database;

public function __construct(Context $database) {
   $this->database = $database;
}

protected function beforeRender() {
    ....
    $this->loadMenu();
}

public function loadMenu() {

        $nodes = $this->database
            ->table('node_language')
            ->where('language_id', $this->session->language_id)
            ->where('node.visibility', 'public')
            ->where('node.level', '2')
            ->order('node.lft, node.parent_id')
        ;
        $this->template->menu = $nodes;

}
?>

4 years ago

Oli
Member | 1219
+
0
-

Constructor injection is bad choice in BasePresenter. It's bad, because you have to use it in every child presenters. Use injectMethod or ineject annotation insted.

It would solve your problem. I gues, you forgot call parent::__construct in ErrorPresenter, don't you?

4 years ago

silviustan
Member | 17
+
0
-

You are right. Error presenter doesn't call parent::__construct. I will try to use inject annotation. Thank you for your help!

4 years ago

silviustan
Member | 17
+
0
-

Works perfect now! Thanks again!