Jump to content
k07n

Wire404Exception() and _init.php

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?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Sounds like a good way to deal with it. That way users trying to get an ID that doesn't exist would simply get your default home page. Makes sense.

Share this post


Link to post
Share on other sites

<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

Share this post


Link to post
Share on other sites

If using init.php it can get executed multiple times if you use render() to render something in your templates.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Moritz Both
      Greetings!
      For our PW project we use markup regions and, for one template, url segments. The documentation recommends throwing a new Wire404Exception() from the template when the code concludes that the url segments from the request are invalid, and so we do.
      However, the 404 page is not displayed properly. Viewing the page source in the browser we can see that the original, unmodified markup region contents from our _init.php file is prepended to the correct html output, messing the whole page up.
      Any advice is greatly appreciated.
    • By Thorsten
      I'm using "throw new Wire404Exception();"  in my template, it worked fine. After Migrating to an other server and Upgrading to PW 3.0.62 I got an Error "Class 'Wire404Exception' not found" . I spent the day yesterday unsuccessfully to understand the reason. Is there any reason, why PW can't find this class? The Exceptions.php in wire/core exists.
       
      I beg your pardon, if this topic should be discussed here yet - the search items 'Wire404Exception' and 'not found'  leed to many other results not related to the class itself.
    • By dreerr
      Dear Communiy,
      My template basic-page does have URL segments activated. The segments are used to handle a different URL for the files attached. If a file is not found a Wire404Exception() is thrown. Sadly PagePathHistory does not handle it correctly (in my opinion) and old URLs no longer are redirected to the new ones.
      wire/modules/PagePathHistory.module:117
      /** * Hook called upon 404 from ProcessPageView::pageNotFound * */ public function hookPageNotFound(HookEvent $event) { $page = $event->arguments[0]; // If there is a page object set, then it means the 404 was triggered // by the user not having access to it, or by the $page's template // throwing a 404 exception. In either case, we don't want to do a // redirect if there is a $page since any 404 is intentional there. if($page && $page->id) return; [...] The documentation makes sense here but is the opposite of the desired effect.
      Only solution for my problem was to disable the if-Statement, is there a better way to do this?
×
×
  • Create New...