Jump to content

Wire404Exception() and _init.php


k07n
 Share

Recommended Posts

Hi!

I rebuilding site from another CMS and need to redirect some old pages to new ones.

Old scheme of urls is site.com/index.php?id=123

I have this in _init.php:

$id = (int)$input->get->id;

if ($id > 0) {
  
  $redirect_page = wire('pages')->get("parent=/tools/old2new/, template=old2new-redirect, name=$id");
  
  if ($redirect_page instanceof NullPage) {

    throw new Wire404Exception();

  } else {

    $url = $redirect_page->redirect_to->url . $redirect_page->param; 
    wire('session')->redirect($url);

  }
}

So, Wire404Exception() -- throw me this:

Fatal error: Exception: (in \www\site\templates\_init.php line 21) #0 \www\wire\core\TemplateFile.php(139): require() #1 [internal function]: TemplateFile->___render() #2 \www\wire\core\Wire.php(359): call_user_func_array(Array, Array) #3 \www\wire\core\Wire.php(317): Wire->runHooks('render', Array) #4 \www\wire\modules\PageRender.module(337): Wire->__call('render', Array) #5 \www\wire\modules\PageRender.module(337): TemplateFile->render() #6 [internal function]: PageRender->___renderPage(Object(HookEvent)) #7 \www\wire\core\Wire.php(359): call_user_func_array(Array, Array) #8 \www\wire\core\Wire.php(317): Wire->runHooks('renderPage', Array) #9 \www\wire\core\Wire.php(381): Wire->__call('renderPage', Array) #10 \www\wire\core\Wire.php(381): PageRender->renderPage(O in \www\index.php on line 216

But if I use exception from another template it working fine and render 404 page. 

Please help to fix it and maybe some suggestion to "redirect" code?

Link to comment
Share on other sites

PHP doesn't like Exceptions in included files and (assuming that it's prependTemplateFile) _init.php is included during page rendering. I'd suggest putting together a simple module for this.

Edit: you could, probably, also do this in your home template, right? Assuming that old URL's were always "/?id=*", they should always end up on your home page and that should work just as well.. or even better? :)

Edited by teppo
  • Like 1
Link to comment
Share on other sites

Edit: you could, probably, also do this in your home template, right? Assuming that old URL's were always "/?id=*", they should always end up on your home page and that should work just as well.. or even better? :)

You right, just one template needed! Dumb me ^_^.

10x

Link to comment
Share on other sites

A 404 exception will interrupt the render process and render the 404 page instead of the requested one along with a 404 header and url stays the same. So the render of the 404 page in the background calls the _init.php again.

When using the prepend file _init.php you always have to keep in mind that it's called on every $page->render() (you might also do manually in a template to render another page). Depending what you do in the _init.php it may cause trouble cause it's called multiple times for a request.

  • Like 1
Link to comment
Share on other sites

A 404 exception will interrupt the render process and render the 404 page instead of the requested one along with a 404 header and url stays the same. So the render of the 404 page in the background calls the _init.php again.

When using the prepend file _init.php you always have to keep in mind that it's called on every $page->render() (you might also do manually in a template to render another page). Depending what you do in the _init.php it may cause trouble cause it's called multiple times for a request.

Thanks, guys, for the great explanation. It's very helpful.

But I think now why am I wanted to throw 404 to the simple get parameter? I have to check for IDs which I want to redirect and just leave others alone  without any redirections. Am I right?

Link to comment
Share on other sites

  • 4 months later...

<ashamed> Still didn't got it </ashamed>

tried to throw a 404 if urlSegment1 isn't pdf as mentioned when activating urlSegments

if($input->urlSegment1 && $sanitizer->pageName($input->urlSegment1) != 'pdf') throw new Wire404Exception(); 

tried in _init.php, _main.php and in the page template as well nothing works

It says "Fatal error: Cannot redeclare myFancyFunctions() .."

Update:

changed "include("./includes/functions.php");" to "include_once("./includes/functions.php");" seems to work :-)

Or is there anything I'm missing or a better way?

Edited by Can
Link to comment
Share on other sites

  • 2 months later...
Thanks, guys, for the great explanation. It's very helpful.

But I think now why am I wanted to throw 404 to the simple get parameter? I have to check for IDs which I want to redirect and just leave others alone  without any redirections. Am I right?

Hi.

It was not very good idea. Search engines don't like when several addresses show page with same content.

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