Symfony custom error page

Reading symphony docs http://symfony.com/doc/current/cookbook/controller/error_pages.html#replacing-the-default-exceptioncontroller

I added exception_controller line into /app/config/config.php. Other parts were already there when installed symphony.

Twig Configuration

twig:
debug: “%kernel.debug%”
strict_variables: “%kernel.debug%”
exception_controller: ApplicationUserBundle:Exception:showException

Then create this class

namespace Application\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ExceptionController extends Controller {
      public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null) {
      return 'blah1';   
      }
}

I have this in /web/app.php

$kernel = new AppKernel('prod', false);
//$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = ApacheRequest::createFromGlobals();
$response = $kernel->handle($request)->send();
$kernel->terminate($request, $response);

But it doesn’t work. I get this instead. Please advice. Googling did not help much!
I expect to get my controller when 404/ResurceNotFound occurs.

Fatal error: Uncaught exception ‘Symfony\Component\Routing\Exception\ResourceNotFoundException’ in /home/[username]/public_html/testfolder/app/cache/dev/appDevUrlMatcher.php:225 Stack trace: #0 /home/[username]/public_html/testfolder/vendor/symfony/symfony/src/Symfony/Component/Routing/Matcher/UrlMatcher.php(112): appDevUrlMatcher->match(‘/he’) #1 /home/[username]/public_html/testfolder/vendor/symfony/symfony/src/Symfony/Component/Routing/Router.php(240): Symfony\Component\Routing\Matcher\UrlMatcher->matchRequest(Object(Symfony\Component\HttpFoundation\ApacheRequest)) #2 /home/[username]/public_html/testfolder/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php(125): Symfony\Component\Routing\Router->matchRequest(Object(Symfony\Component\HttpFoundation\ApacheRequest)) #3 [internal function]: Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), ‘kernel.request’, Object(Symfony\Component\EventDispatcher\ContainerAwareEventDis in /home/[username]/public_html/testfolder/app/bootstrap.php.cache on line 2986

Double check your controller method names. “showException” != “show”

I don’t want to use twig but php templating so instead of this i should create an errorHandler as stated on docs, but my app.php does not have controllerResolver object, I just use default app.php that symfony installs for me so how can I add an exceptionmanager/errorEventListener to this to handle custom error pages? Please advice.

Once you have your own exception controller, then you can render your response however you like, including return $this->render('whatever_you_want.html.php');

EDIT: But if you really want to make your own exception event listener, then read Working with the kernel.exception Event, and also follow and read any links within that section.

Thanks for trying to help. Actually I did read the docs before posting here but as mentioned here:

It uses below to submit an exceptionHandler, right?
$kernel = new HttpKernel($dispatcher, $resolver);

but in my app.php I have:

$kernel = new AppKernel('dev', false);
//$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = ApacheRequest::createFromGlobals();
$response = $kernel->handle($request)->send();
$kernel->terminate($request, $response);

so how to catch kernel.exception and call the exceptionController in this code?

No, not at all.

You need to be careful to distinguish between Symfony the components and Symfony the framework. That page you just linked to is about the HttpKernel component. That’s why the sample code is instantiating its own copy of the HttpKernel.

If you’re using the framework and want to listen to the exception event, then read the link I posted above, and also follow and read any links within that section.

Muaaaa! Thank you very much. As you gave the hint that I should not confuse framework with component. I was able to do this. First http://symfony.com/doc/current/book/internals.html#kernel-exception-event
I understood that I should create a class::method() like:
ExceptionListener::onKernelException()
Then with this:
http://symfony.com/doc/current/cookbook/service_container/event_listener.html
http://symfony.com/doc/current/book/service_container.html
I did setup this in my service.php

$container
    ->register('kernel.listener.your_listener_name', 'Acme\DemoBundle\EventListener\AcmeExceptionListener')
    ->addTag('kernel.event_listener', array('event' => 'kernel.exception', 'method' => 'onKernelException'));

It works great now! Just two questions please:

  1. I know that’s no matter what ‘your_listener_name’ is, what what is the purpose of define a name: kernel.listener.your_listener_name
    I think I don’t need to call this service container manually, so why should I define a custom name for this listener?
  2. Whenever I do any changes I should clean cache, that’s very annoying in development environment. how to disable it? I commented:
    //$loader = require_once DIR.‘/…/app/bootstrap.php.cache’;
    But I get blank page instead. these are commented too:
    //$kernel->loadClassCache();
    //$kernel = new AppCache($kernel);
    so how to disable caching in development as this is annoying?

Every service gets a name, regardless if you ever reference it by name or not.

By running app_dev.php instead of app.php.

Thanks,

Reading these docs:
http://brentertainment.com/other/docs/book/doctrine/dbal.html
http://brentertainment.com/other/docs/book/doctrine/orm.html
http://brentertainment.com/other/docs/reference/configuration/doctrine.html

I created this and added to my bundle services.php
http://pastebin.com/kzbPdpyg

But I get the error:

[Symfony\Component\DependencyInjection\Exception\LogicException]
Container extension “doctrine” is not registered

What more I missed to do?
Also I have no idea what ‘read’ and what ‘host’=>‘from_user_config.db’ is, I copied/pasted from a third-party application and how to inject db credentials from a separate file into this services.php?

I googled the problem and found this
However mine is about ‘doctrine’, as cache delete via console fails, I deleted cache folder manually, and still have the problem what else should I do?

I noticed my pastebin link in previous reply was expired. I updated that post with new pastebin link. please advice.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.