I need to change display error off in Symfony framework - php

I want to change symfony display error reporting to off because right now when error occurred it not display good it always showing my code and i think it's not good. I tried several ways but nothing works I want to tell you i don't have access to ssh terminal. i need to do this task with coding.

Assuming you have taken care of those errors, but need to to override the error page, or in other words you want to customise the error page, you will need to do following things.
Create a Template to display errors
{% extends 'base.html.twig' %}
{% block body %}
<h1>Page not found</h1>
<p>The requested page couldn\'t be located. Checkout for any URL
misspelling or return to the homepage.
</p>
{% endblock %}
Override the default controller
twig:
exception_controller: App\Controller\ExceptionController::showException
Or if you want to create a new exception controller from the scratch,
# config/services.yaml
services:
_defaults:
# ... be sure autowiring is enabled
autowire: true
# ...
App\Controller\CustomExceptionController:
public: true
arguments:
$debug: '%kernel.debug%'
I have extracted this from the Symfony documentation. I recommend you to throughly read the Symfony documentation and familiarise yourself in this area. https://symfony.com/doc/current/controller/error_pages.html
Thank you.

Related

Symfony 3 - FOSUserBundle - register.html.twig - template not found

I'm pretty new to Symfony development and i just tried to use the FOSUserBundle. I followed the guide Getting Started With FOSUserBundle on the official Symfony website.
Afterwards i wanted to test the functionality of the installation and visited the link www.linktomytestproject.dev/login which worked flawlessly. But when i tried to visit www.linktomytestproject.dev/register to test the registration functionality, i got the following errormessage:
Unable to find template "register_content.html.twig" (looked into: /home/vagrant/mytestproject/app/Resources/views, /home/vagrant/mytestproject/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form) in #FOSUser/Registration/register.html.twig at line 4.
500 Internal Server Error - Twig_Error_Loader
I checked the file register.html.twig, which contains the following code:
{% extends "#FOSUser/layout.html.twig" %}
{% block fos_user_content %}
{% include "register_content.html.twig" %}
{% endblock fos_user_content %}
When I change the third line into:
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
The file is found correctly and i can use the registration functionality as intended.
But the change i made is inside the vendor folder, which obviously isn't affected by any means of version control. So i guess there has to be a place somewhere in the configuration files, where this file/foldermapping is affected.
I would really appreciate it, if someone could help me out with this question, even though this is probably a really basic problem.
First override the FOSUserBundle http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html
After that, you can create view folders like in vendor and you can use views like that;
{% include "YourBundle:Registration:register_content.html.twig" %}
It's a bug a issue is open on github
Is a bundle issue, in my project I change on the file
\vendor\friendsofsymfony\user-bundle\Resources\views\Registration\register.html.twig
this
{% include "register_content.html.twig" %}
for this
{% include "#FOSUser/Registration/register_content.html.twig" %}

Symfony 2 : Flash message on login / logout

Using Symfony 2.6, I am looking for the best way to add a flash message on login / logout in my application. I precise that in each case I don't want to modify the Response (I don't need to).
Here are my solutions :
Login:
1. First solution :
I can use the security.interactive_login event or the security.authentication.success event. In both case I can implement a listener to add a flash message when the event occurs.
2. Second solution :
I can create my own LoginSuccessHandler that implements the AuthenticationSuccessHandlerInterface, but in this case I have to return a response. Moreover, I am not familiar with this method and I am afraid to overwrite the default success handler. Is that correct ?
Logout:
I can create a LogoutListener that implements the LogoutHandlerInterface. In this case I am also afraid to overwrite the defaut logout handler.
Thank you for your proposals.
Use the FOSUserBundle login/logout logic etc. already implemented...
Thereby you will have the possibilty to show errors on the login-check redirect:
To do this you must return the error to the twig
$session->getFlashBag()->add('error', 'Error message');
And in the twig you will need a block like this to show errors, if the session is holding any:
{% block flashes %}
{% if app.session.flashbag.peekAll|length > 0 %}
{{ session_flash() }}
{% endif %}
{% endblock flashes %}

Error pages Symfony2 with locale

I'm trying to translate 404 pages on Symfony2, but it's not working. With 500 error pages it works great.
For example, when i try to access the page: /es/not-found it shows the 404 page, but when i access /en/not-found, it gives me the SAME page, although i have translated the error message on my messages-en.yml.
It seems it's always accessing the messages-es.yml file, because i've configured Symfony with this default locale (es).
If i print {{ app.request.locale }} on my Twig template, it doesn't give me anything.
I cleared the cache several times, yes :).
Thanks in advance!
If the localization configuration is properly working, you should be able to print the locale on Twig. With something like this:
{% if app.request.locale == 'en' %}
English 404 message
{% else %}
Other 404 message
{% endif %}
If your Symfony version is less than 2.1 you must use app.session.locale instead of app.request.locale
Nevertheless is a good practice to always create custom 404 and any other error page you need:
http://symfony.com/doc/current/cookbook/controller/error_pages.html
The most powerfull way is using this method:
Replace the default exception controller twig.controller.exception:showAction with your own controller and handle it however you want (see exception_controller in the Twig reference). The default exception controller is registered as a service - the actual class is Symfony\Bundle\TwigBundle\Controller\ExceptionController.
This way you can even have different templates for each country.
Kind regards.

Symfony2 - Include template from current bundle

I've not been able to find anything useful about this in the Twig or Symfony2 documentation, so thought I would ask here.
Does anybody know if it's possible to include a Twig template in Symfony2 relative to the current bundle, without specifying the name? Something along these lines:
{% include .:Foo:bar.html.twig %}
I'm just a bit fed up of having to enter the long, ugly bundle name when they're all in the same bundle. Also means if the bundle name ever changed for whatever reason, I'd have to find & replace every single include.
Back in the days when I was using bundles, I came up with a quick solution that you could base upon:
{% set bundle = app.request.get('_template').get('bundle') %}
{% set controller = app.request.get('_template').get('controller') %}
{% include bundle ~ ':' ~ controller ~ ':foo.html.twig' %}

How to use dump in TWIG?

I add for template (index.html.twig) simply:
{{ dump(product) }}
and i have error:
The function "dump" does not exist in AcmeStoreBundle:Default:index.html.twig at line 2
Why this function is not enable, and how can i enable this?
You need to configure the debugging extension:
# app/config/config.yml
services:
acme_hello.twig.extension.debug:
class: Twig_Extension_Debug
tags:
- { name: 'twig.extension' }
Per the link mentioned above, Twig debugging is set to work by default in Symfony 2.5+ running Twig 1.16+, and the custom service definition is not necessary. See this answer for more details.
When you configure it like #meze said, you can display all custom variables:
<h1>Variables passed to the view:</h1>
{% for key, value in _context %}
{% if key starts with '_' %}
{% else %}
<pre style="background: #eee">{{ key }}</pre>
{{ dump(value) }}
{% endif %}
{% endfor %}
You can use my simple plugin to convenient inspect your variables:
Twig Dump Bar
Symfony 2.7+ UPDATE:
The DebugBundle allows greater integration of the component into the Symfony full-stack framework. It is enabled by default in the dev and test environment of the Symfony Standard Edition.
Check the VarDumper component and its integration on Twig.
OLD ANSWER:
I would like to suggest a non-native solution. You'll need a third-party bundle, but the final result will be great!
Improvements to the dump version:
all dumps are styled
you can provide the max nesting level to avoid memory issues with large objects
very useful helpers like ldd(), which is an alias for the classic "dump and die"
it has a console dumper (eg. php app/console ladybug:dump "Symfony\Component\HttpFoundation\Request")
it provides an integration with the Symfony Profiler
it automatically detects Symfony, Doctrine, Twig and Silex classes, and links them to the official documentation
Here the links:
knpbundles
github

Categories