Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Create a 404 Error page for NotFoundHttpException #1

Closed
ElectricMaxxx opened this issue Feb 2, 2014 · 11 comments
Closed

Create a 404 Error page for NotFoundHttpException #1

ElectricMaxxx opened this issue Feb 2, 2014 · 11 comments
Milestone

Comments

@ElectricMaxxx
Copy link
Member

Instead of the Exception i would be good for SEO that the user see an error-page, with information for navigating the content or hints for equal content.

@dantleech
Copy link
Member

Should this not be handled by the application rather than the bundle?

Although an interesting approach for this would be to query the routes and find those which are closest to the given URL, something like "did you mean .....?".

@ElectricMaxxx
Copy link
Member Author

It is a good way not to show an Exception or the Error itself in production. It is a better way to show a user friendly page with some information to, which should help the user to navigate to other content.

And the only error that should appear, should be a route-not-found-error caused by older links or (in most cases of my customers) old bookmarks. In this case the user should get informations like:

  • what can i see else
  • what can be equal content (as @dantleech said)
  • start from beginning -> link to Homepage
  • ...

To design this page is a task of the application, but to show it could be a task of the SeoBundle.

The 404 is not for the user, its for google (or others, are there others? g). By this google knows it too, that the route does not exist anymore.

@ElectricMaxxx
Copy link
Member Author

I think a good way would be to add a listener on that exception. or is there a Kernel event or better a event from our own routing bundle?
But all benefits of the chain router would be a problem in here. Cause when can i call a route "not found"? after all chained routers failed. So the only thing to listen to would be the exception or an kernel event.
Then i would call either a default controller to render a simple template Or call a costum controller for havier work. Both can be defined in config in an equal way as the routing bundle does it.
Is there any reason to create more than one listener? Means more than on error to show or derect? Or would it be better to create contexts with a own setting for each?

@dantleech
Copy link
Member

Adding an exception listener is pretty easy in Symfony, this is an example from some some code I work on:

        $container->register('yprox.exception.listener', 'Ylly\CmsBundle\Exception\ExceptionListener')
            ->addArgument(new Reference('templating'))
            ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'method' => 'onKernelException'));

So you could add that listener (giving it a high priority) and if the Exception is of the type thrown by the ChainRouter, or indeed if it is any HttpNotFoundException you can do whatever logic you need in the listener and set the response object on the exception, e.g. something like this:

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $this->buildException($event);
        $event->setResponse(new Response($this->getTemplating()->render('CmsBundle:AdminException:exception.html.twig', array(
                'exception' =>  $event->getException(),
        )), 404));

        return true
    }

@ElectricMaxxx
Copy link
Member Author

right, thanks, I just red the symfony docs about that theme :-).
This will be the way i gonna dot it (i hope this evening)

One word to the configs. There a two ways in my mind. One with one error page:

cmf_seo:
   ...
   error_page:
        by_template: AcmeDemoBundle:Seo:error.html.twig    #one of both
        by_controller: acme_demo.controller.error:showAction #one of both
        html_state: 404

Or a way with several contexts to handle different errors:

cmf_seo:
   ...
   error_pages:
        on_route_exception:
          by_template: AcmeDemoBundle:Seo:error.html.twig    #one of both
          by_controller: acme_demo.controller.error:showAction   #one of both
          listen_to: RouteNotFoundException  #todo: right FCN
          html_state: 404
      on_other_stuff
          by_template: AcmeDemoBundle:Seo:other_stuff.html.twig
          by_controller: acme_demo.controller.error:otherStuffAction
          listen_to: OtherException #the FCN
          html_state: 404

by setting the listen_to property i can filter the Exception and handle different behaviors.

@dbu
Copy link
Member

dbu commented Feb 10, 2014

i think the twig bundle allows all you usually need: http://symfony.com/doc/current/cookbook/controller/error_pages.html - you can already overwrite the template per error status. things that go further are very specific anyways. it makes sense to explain the importance of setting up good error pages in the doc chapter, and we can also link to the exception listener doc of symfony for people that need to go further. but adding our own error handling makes little sense imho.

@ElectricMaxxx
Copy link
Member Author

You mean, just a part in the docs, instead of an own handling?
Indeed, you can easy overwrite the error_page from the twig bundle or listen to exceptions.
Ok, that makes sense from the view of "hours of work to invest". And right there is a solution given, but with some (not much) work for the developer of the application. But wouldn't it be great to give some settings and get the behavior? I know we need to teach everybody, that the action behind the scenes is nothing morre than http://symfony.com/doc/current/cookbook/controller/error_pages.html or that http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

But there is an other important thing, and that is SEO:
The hints for the user or even the search enginge. Those hints can be links like:

  • What else to see
  • Equal content (directly from our content tree -> power of the cmf)
  • other hints

I red a realy good article for UX in the magazine "der web designer" Oct/12 "Benutzerfreundliche Fehlerseiten". Sounds like a design article but is a SEO article too. Cause ErrorPages are show on search engine previews too. And if there is just a blank page or the servers default 404 page it can very bad if the content is a product in a webshop for example.

(sorry if am to enthusiastic in discussion)

@dbu
Copy link
Member

dbu commented Feb 14, 2014

okay, if we manage to do a semantical error page that proposes options (maybe looking for path fragments, urls with other locales, search with url segments) that would indeed have added value. but i would postpone such things to a 1.1 version (and it will remain very specific as people will want to tune this a lot to their specific use case)

@ElectricMaxxx
Copy link
Member Author

Yes i thing the SeoBundle itself is a thing for after 1.1, or?

@dbu
Copy link
Member

dbu commented Feb 14, 2014

there is no rush. but i think getting something simple but clean and
stable out soon would be really cool. then we can add more features
later. otherwise its a constant work in progress and people start to use
it anyways and get confused when things change (as happened with the
RoutingAutoBundle that stayed in sort of alpha-modus for a very long time)

@ElectricMaxxx
Copy link
Member Author

solved by #192

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants