1

I'm trying to create a custom exception controller in php Symfony 4. Essentially what I want to do is to have a default route that gets called when no other routes match.

I'm trying to follow the suggestions here: https://symfony.com/doc/current/controller/error_pages.html#custom-exception-controller

I've created the twig.yaml file inside configs>>pacakges:

twig:
    exception_controller: App\Controller\ExceptionController::showException

I've also created the ExceptionController class:

<?php
namespace  App\Controller;


use Symfony\Component\HttpFoundation\Response;

class ExceptionController
{

    public function showException()
    {
        $response = new Response();
        $response->setContent("Some random text");
        $response->headers->set('Content-Type', 'text/plain');
        $response->headers->set('Access-Control-Allow-Origin', '*');
        $response->setStatusCode(200);
        return $response;
    }
}

When I visit a url that does not exist I'm expecting to see the text "Some random text" but instead I get the default symfony not found page:

enter image description here

Any ideas what I might be doing wrong?

1
  • I had another look at this - I was able to create a new Symfony 4 instance and override the exception controller without any issue. One thing I noticed in your question: you said you created a twig.yaml - this should exist already. Also, you said you created it under configs - this should be config. Can you clarify the path of your twig.yaml? Commented Sep 29, 2018 at 8:53

2 Answers 2

1

You mentioned that you created a twig.yaml under configs/packages.

Two things:

  1. This should be config, not configs
  2. twig.yaml should already exist, there is no need to create this file.

My suspicion is that if you created this file, you've created a file in the wrong place and Symfony is not picking up your configuration change. Instead, add your configuration change to the existing twig.yaml file under config/packages.

Once you add your custom exception_controller path to this existing (assumed default) twig.yaml it should look something like this:

twig:
    default_path: '%kernel.project_dir%/templates'
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    exception_controller: App\Controller\ExceptionController::showException

I tested this with a fresh install and it works as expected.

Hope this helps :)


Your method should be called showException and not endpoint as you defined in your twig.exception_controller configuration.

Sign up to request clarification or add additional context in comments.

3 Comments

I've actually tried that as well but it did not work. Will update the original post with "showException"
Thank you, this post led me to find the answer. The documentation on setting up the exception controller only applies to symfony projects created using symfony/website-skeleton and not symfony/skeleton. The latter which is used for just setting up the minimum project for api calls.
Interesting. Good to know! Glad you have it sorted :)
0

Did you try to use routing for routes ? Rooting

1 Comment

I'm not exactly sure what you mean by routing for routes? If you are are referring to mapping end points to urls all my main endpoints are mapped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.