1
votes

I am using symfony 1.4.

I need to redirect certain urls when 404 error occurs. Let's say user is looking for a url http://example.com/oldurl and it doesn't exist I want to check if this url is set for redirect. If url is set to be redirected I would like to redirect it to that url.

QUESTION: Which event should I hook into to get info about the requested url, that got redirected to 404 error page ?

P.S We only want to check for redirection if page doesn't exist. We do not want to run "the check" for every request, only when page not found!

thanks a lot!

2

2 Answers

1
votes

Symfony fire an event each time a page is not found: controller.page_not_found.

You can find it in the documentation: http://www.symfony-project.org/reference/1_4/en/15-Events#chapter_15_sub_controller_page_not_found

Edit:

You can retrieve the url in the method that listen to this event by calling the context. With something like that:

$context = sfContext::hasInstance() ? sfContext::getInstance() : null;
if(null !== $context)
{
  $requested_url = $context->getRequest()->getUri();
}

You can give a closer look to the plugin sfErrorNotifierPlugin, it catches exception and 404 error and send an email for a report. Look at how they handle 404 error.

0
votes

I don't know where to hook exactly inside the execution stack, but I know the general cleaner way through routing. If you are doing routing without still having in code the default routing rule (which you shouldn't by practice since), then it would be as simple as the following in your app routing.yml (at the end):

catch_all:
  url: /*
  param: { module: yourModule, action: handleNotFound }