Jump to content

PageNotFoundException leads to redecleration of _init.php functions


oap
 Share

Recommended Posts

In my home.php (a template, as you'd expect for the homepage) then I take a look at the URL segments. I want to provide some shortlinks, e.g. example.com/php redirects to example.com/languages/php/. That all works. But if I want to display a 404, by using "throw new PageNotFoundException();" then things go pearshaped:

 Fatal error: Cannot redeclare renderNav() (previously declared in /var/www/pw/public_html/site/templates/_init.php:20) in /var/www/pw/public_html/site/templates/_init.php on line 20

I'm not sure how to get around this? (or exactly why it's coming up)

This is the case if I try to declare a 404 at the top of any template.

Link to comment
Share on other sites

  On 8/18/2015 at 9:48 AM, LostKobrakai said:

I'm not sure, where you got that exception, but normally pw uses Wire404Exception() to throw 404 errors.

Thanks for pointing that out, I think I took it from this forum page but didn't fully read the later posts!

But unfortunately Wire404Exception() produces the same problem.

Link to comment
Share on other sites

So I am using a blank template for a page (which is only calling the title of the page). It is also not including the header or footer in the template.

I am then calling the page in my basic template using:

		<?php echo $page->import_page->render();  ?>		

However, when I go to refresh the page again, I get an error:

"Compile Error: Cannot redeclare renderNav() (previously declared in /Applications/MAMP/htdocs/ProcessWire/site/templates/_init.php:20) (line 49 of /Applications/MAMP/htdocs/ProcessWire/site/templates/_init.php) 

Has anyone had the same issues?

Edited by LostKobrakai
Merged into earlier topic
Link to comment
Share on other sites

You're both probably including "renderNav()" somewhere in your _init.php. I wasn't sure about 404 exceptions, but I'm about $page->render(), where the init.php is prepended a second time within a single request, and one cannot create a function of the same name twice. I suggest moving helper functions in an own file and just include them in _init.php via include_once. This will make sure the function is never created more than once.

Link to comment
Share on other sites

  On 8/21/2015 at 2:29 PM, LostKobrakai said:

You're both probably including "renderNav()" somewhere in your _init.php. I wasn't sure about 404 exceptions, but I'm about $page->render(), where the init.php is prepended a second time within a single request, and one cannot create a function of the same name twice. I suggest moving helper functions in an own file and just include them in _init.php via include_once. This will make sure the function is never created more than once.

Thanks very much for the suggestion of how to get around it.

It seems a bit odd though, since rendernav() came in the sample site included in the PW installation - i.e. it is expected that _init.php will be used for this sort of thing.

Regardless - thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...