Markdown

[Symfony] app_dev.php profiler throws 404

https://stackoverflow.com/questions/16800692/symfony-404-not-found/34048364#34048364
https://stackoverflow.com/questions/16243746/starting-with-symfony-2-app-dev-php-works-app-php-404s
https://stackoverflow.com/questions/30336232/i-cant-access-to-my-web-site-symfony-you-are-not-allowed-to-access-this-file

According to my own experience; this behavior happens when you change the standard behavior of your routes. For example, in my case I was using kernel.request listener to create a language detection mechanism which supposed pick the best possible language for the user based on her/his browsers supported language.
I was using {_locale} prefix in all my routes located in app/config/routing.yml
some_route:
    resource: "@MyBundle/Resources/config/routing.yml"
    prefix:   /{_locale}/
This way I was able to quickly add at the beginning of my routes the language code and in my listener class I was redirecting the client to a route prefixed with an existing language code regardless the client requested the route with or without the locale.
For example, if my site had only "en" and "de" as languages and if the user requested
http://www.mysite.com/somepage
or
http://www.mysite.com/fr/somepage
I was redirecting the client to
http://www.mysite.com/en/somepage
So whenever there was an error in my application the profiler route was unaware of this situation and was trying to show itself from a route that has no {_locale} prefixed. And this was causing a 404 error.

---


The solution was simple:
I opened the app/config/routing_dev.yml file and modified the all the routes as below:
_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /{_locale}/_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /{_locale}/_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /{_locale}/_configurator
Notice the /{_locale} part.
So the problem is fixed for me.

---

My problem was that kernel.terminate event was executing for more than 10 seconds. It was very hard to spot, because profiler was not measuring timeline for this event.
When I realised this, I immediately narrowed the problem down to one of my event listeners having abnormally long running __construct method. This delayed profile file printing for too long, which caused a Javascript timeout error after 5 retries.
After fixing the offending listener, kernel.terminate event went fast and the problem was gone immediately.
You can quickly see your attached listeners using bin/console debug:event-dispatcher kernel.terminate.
---
It's often problem with event subscriber/listener. Try run
console debug:event
And check for errors

https://stackoverflow.com/questions/16243746/starting-with-symfony-2-app-dev-php-works-app-php-404s


---


1down voteaccepted
i found the problem. i thought it was strange that nothing was showing up in the prod log, so i thought maybe something was redirecting me, causing me to miss the app.php file entirely.
it turns out, this was the problem. i emptied the contents of the .htaccess file that was in the web folder (the one that symfony came preconfigured with) and then everything magically started working.
shareimprove this answer

留言