FOSUserBundle with Symfony 3.0 - symfony

I'm trying to get FOSUserBundle up and running after upgrading Symfony from version 2.6 to 3.0. On 2.6 I basically had a site, css and js we're handles by assetic, FOSUserBundle was up and running an I had configured SQLite via doctrine.
So before picking this project up again I did the upgrade because 2.6 was no longer supported.
Because I'm new to Symfony everything I did is more or less copied from the documentation. FOSUserBundle is showing up as an enabled bundle in dev-mode. Alle my code is in /src/AppBundle. I'm using the 2.0-dev version of FOSUserBundle (e770bfa to be precise).
Here's a part of my template:
<li>
{% if is_granted("IS_AUTHENTICATED_REMEMBERED") %}
<a href="{{ path('fos_user_security_logout') }}">
{{ 'layout.logout'|trans({}, 'FOSUserBundle') }}
</a>
{% else %}
<a href="{{ path('fos_user_security_login') }}">
{{ 'layout.login'|trans({}, 'FOSUserBundle') }}
</a>
{% endif %}
</li>
At 2.6 I got
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.yml"
in my /src/AppBundle/config/routing.xml. If I keep it this way I can't even load the site because of an Exception
"Unable to generate a URL for the named route "fos_user_security_login" as such route does not exist."
After putting it in /app/config/routing.yml my site shows and the other routes defined in /src/AppBundle/config/routing.xml work. So it was nothing wrong with this file.
When the site shows up the translation of the links is broken.
{{ 'layout.login'|trans({}, 'FOSUserBundle') }}
Shows up as "layout.login" instead aus Login as before with 2.6. And if I click the link to log in (the path is as expected /login). Symfony tells me:
Unable to find template "AppBundle:Pages:login.html.twig".
I don't get why it's looking for it in the AppBundle folder? According to what I read it should look for it in the FOSUserBundle folder or in /app/Ressources/FOSUserBundle/... which is where I put it to override the default template. I confirmed that this is still the way to override templates even with version 3.0.
I also tried to put the template in the AppBulde/Pages/ folder. Then it finds the template but it's still not working.
I cleared the cache multiple times (this solved another problem with assetic).
It look for me as if I'm missing a significant part to get FOSUserBundle working. Any suggestions on what I might have overlooked?

FOSUserBundle seems to be use 'xml' files for routing, and you have include an 'yml' file in your routing.xml:
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.yml"

Related

Asset_url in production environment Symfony 3

I have this doubt about URLs of assetics who I generated.
On the view, this was what I did:
{% block stylesheets %}
{% stylesheets
'assets/vendor/bootstrap/dist/css/bootstrap.min.css'
'assets/vendor/font-awesome/css/font-awesome.min.css'
'assets/vendor/dist/css/*.css'
output = 'bundles/compiled/css/app.css' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endblock %}
{% javascripts
'assets/vendor/jquery/dist/jquery.min.js'
'assets/vendor/bootstrap/dist/js/bootstrap.min.js'
'assets/vendor/dist/js/wow.min.js'
'assets/vendor/dist/js/slider.js'
'assets/vendor/dist/js/jquery.fancybox.js'
'assets/vendor/dist/js/owl.carousel.min.js'
'assets/vendor/dist/js/main.js'
'assets/vendor/dist/js/modernizr-2.6.2.min.js'
output = 'bundles/compiled/js/app.js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
In dev environment, the app doesn't call these outputs links who assetic:dump generate, which is:
bundles/compiled/css/app.css
and
bundles/compiled/js/app.js/
both after /web folder.
In prod, I can't reach these locations. I get and 404 on
http://link.com/bundles/compiled/css/app.css for example.
So, how can I configure my config.yml for reach that path in prod environment?
This is what I have:
assetic:
debug: '%kernel.debug%'
use_controller: '%kernel.debug%'
filters:
cssrewrite: ~
**Edit:**I figured out the problem. My assetic's config was alright, the problem was in VirtualHosts file (modrewrite line, to be specific), which was only accepting ".php" extension files and web/ folder permissions.
That's the whole point of using AsseticBundle! When you are in prod mode, you need to dump the asset files. Have a look here.
php bin/console cache:clear --env=prod //clear the cache for prod environment
php bin/console assetic:dump --env=prod --no-debug
The result will be the concatenation of all your css stylesheets, in one single result file (bundles/compiled/css/app.css), and the concatenation of all your js scripts in one single result file (bundles/compiled/js/app.js), for the reason of reducing the requests to the server.
At this point, you won't be able to see, in the source of the page, all the css + js files, except those two from your compiled/ directory.
When you are in dev mode, the default behavior of AsseticBundle is to generate all your css + js files, for debugging purposes. That is, to help you debug your code easily. So at this point you will be able to see all you css+js files, but not the two files from compiled/ directory.
And pay attention, as cssrewrite must be a child of filters. See here the correction you need.

Assetic and domain folder

I'm using Assetic to manage assets in my Symfony2 project. It worked well before i made my application accessible with a domain folder.
before : myapplication.local // Assetic works
now : mydomain.local/myapplication // Assetic doesn't work
The requested css files are called but the filter cssrewrite writes a wrong path for the ressources.
Error :
NetworkError: 404 Not Found - http://www.mydomain.local/Resources/public/images/menu/nav-bg-1.png
The expected URL should looks like http://www.mydomain.local/myapplication/Resources/public/images/menu/nav-bg-1.png
Here is my Assetic Call
{% block stylesheets %}
{% stylesheets
'#Mybundle/Resources/public/css/myfile.css' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
{% endblock %}
I know Assetic say to not use the #-naming when using cssrewrite. But it worked fine without the domain folder, and using "bundles/mybundle/css/myfile.css" does not solve the problem.
If you need anymore information, just ask me ;).
Thank you for your time and you help.
You should use naming without # as said in the documentation. Also you need to dump your assets via assetic every time you change anything with them.
To dump assetic run next command:
php app/console assetic:dump
or
php app/console assetic:dump --env=prod // To generate only files needed for production environment
I managed to fixe this issue by removing the cssrewrite filter and putting all my images, fonts, etc... in web/images or web/fonts, etc... Then in my css, all my path are like "../folder/file"
You can define the assets base URL in your app/config/config*.yml files:
In dev environment: app/config/config_dev.yml
framework:
templating:
engines: ['twig']
assets_base_urls:
http: [http://www.mydomain.local/myapplication/]
In prod environment: app/config/config_prod.yml
framework:
templating:
engines: ['twig']
assets_base_urls:
http: [http://www.example.com/static/]
The cssrewrite filter should remove Resources/public/ from the URL.

How to use the official zurb foundation bundle for symfony2?

my question may seems really pointless or I may look like an idiot, but I'd like to use Zurb Foundation in Symfony2.
I saw a little number of topics about it, but never an real a clear explanation on how to integrate it properly in my project.
I saw an official bundle on packagist. I installed it so. But there is no documentation for using it with symfony2. For example, how to call my css and js files ? From the vendor folder directly ? How to use compass in my symfony project to use it ? Should I use bower directly ? (this seems to me less painfull) And if yes, if I have an already existing Symfony2 project, may I use bower in the root of my project ? in web folder ? anywhere else ?
I'm really lost and would be greatfull if someone could explain me or give me some resources to help me out.
Thanks
For my website I put all my JS/CSS files in vendor NameVendor
In my layout
{% javascripts
'http://code.jquery.com/jquery-2.1.0.min.js'
'#NameBundle/Resources/public/js/*' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% stylesheets
'#NameBundle/Resources/public/css/*'
filter='cssrewrite' output='css/*.css' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
In script tag
$(document).ready(function(){
$(document).foundation();
});
In App/config/config.yml
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ NameBundle ]
filters:
cssrewrite: ~
I failed to use grunt to update my files so do it manually
https://github.com/florianbelhomme/FlobFoundationBundle
I think that's what you're looking for?

Step by Step Guide to Enabling Twig Debug dump() in Symfony

Goal
The big goal is to just print out the variables that are available in the twig form template /views/Form/fields.html.twig so that I can find out WHICH variables are available, AND in particular put a condition in the {% block widget_attributes %} based on the field type (which supposedly exists but for some reason is not accessible, and other suggestions to get the type are warned against).
I just want to see all the variables that are available... and the values they hold. Easy, right?
Lost
So that led me down a lot of rabbit holes, and some helpful articles point out how you can loop through the variables of the current context:
{% block widget_attributes %}
<ol>
{% for key, value in _context %}
<li>{{ key }} :
{% if value is not iterable%}
{{ value }}
{% else %}
{{ dump(value) }}
{% endif %}
</li>
{% endfor %}
</ol>
{% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
{{ parent() }}
{% endblock widget_attributes %}
But this doesn't print out type, and it doesn't actually dump the value if it's not iterable. It kills symfony without any error. So getting debug to work is essential for many reasons.
Enabling Dump
All the suggestions to enable dump, don't work. Twig's website is particularly unhelpful, since it provides no context how or where to load the $twig = new Twig_Environment (and what is up with the latest version being 1.5 at twig but 1.16 in symfony?). Symfony says it will be enabled by default. But it doesn't work.
The app.php (to load the kernel has debug enabled):
$kernel = new AppKernel('dev', true);
This is what is in my config.yml:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
And the other suggestions for enabling in the config_dev.yml don't work either:
imports:
- { resource: config.yml }
# this is from one of the suggestions, but it doesn't work and may be an older method
services:
twig.extension.debug:
class: Twig_Extension_Debug
tags: [{ name: 'twig.extension' }]
Still Lost
So as with so many thing in Symfony, it's powerful and awesome, until it doesn't work, and then there is no documentation on how to make it work. Any help would be appreciated.
I'm running Symfony 2.5, which composer updates to Twig 1.16.
All the suggestions I read in other posts seemed to be for an older version of Symfony and they did not work for me. But Twig debugging is enabled by default in Symfony now. So this is what I did to solve my problem:
1. Upgrade to Symfony 2.5. Edit the /composer.json file and update the symfony version.
2. Update your required dependencies. On the command line run composer update
3. Update Twig. This also automatically updated twig to 1.16 (Symfony requires a minimum version, so if your project requires Twig's latest version of 1.5 you need to require that in our own composer.json file).
4. Load Kernel with Debug On. Make sure the kernel loads with debug turned on in Dev mode, by default this would be in your app_dev.php file (the index file loaded to access your dev mode).
$kernel = new AppKernel('dev', true);
5. Check Config. Make sure twig debug is enabled based on kernel debug mode, edit the config.yml:
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
6. Check Dev Config. Make sure your config_dev.yml imports config.yml (or at least has the relevant config from above).
imports:
- { resource: config.yml }
After doing that, the dump function now works in Twig:
{% block widget_attributes %}
{{ dump(attr) }}
{% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
{{ parent() }}
{% endblock widget_attributes %}
If you try to enable it on prod env.
You should add following into app/AppKernel.php;
$bundles[] = new \Symfony\Bundle\DebugBundle\DebugBundle();
and
in app/config.yml
edit this line;
twig:
debug: %kernel.debug% ========> sure this set true
strict_variables: %kernel.debug%
but keep in mind if your project in live server it can be insecure in terms of
leaking debug information

assetic:dump fails on "less" filter

When I try and dump my assetic managed content for a Symphony2 app, I get the following error:
$ php app/console assetic:dump -e prod
Dumping all prod assets.
Debug mode is off.
[InvalidArgumentException]
There is no "less" filter.
However, as far as I can tell I don't use any assets that require the less filter, certainly a grep of all my twig templates doesn't turn anything up.
One of the dependencies I've installed via composer (Twitter's bootstrap) has some .less templates, but I don't reference them in my twig templates, I just point it at the css version. Will assetic still try and dump them? How can I tell it not to?
For reference this is how I include the css in my template
{% stylesheets filter="cssrewrite"
'../vendor/jquery-ui-css/jquery-ui-css/*css'
'../vendor/twitter/bootstrap/docs/assets/css/bootstrap*.css'
%}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}">
{% endstylesheets %}
hm many possible causes ...
1) some third-party bundle adds an assetic collection dependeing on the less filter in a compiler pass
2) there is an apply-to rule like
assetic:
filters:
less:
apply_to: *.less
... in your assetic configuration i.e. app/config/config.yml
3) there is an asset collection using less filter in your configuration
assetic:
assets:
css_character:
inputs:
- "%kernel.root_dir%/../src/Acme/YourBundle/Resources/public/less/*.less"
outputs:
- css/my.css
filters:
- less
4) one of your third-party bundles provides a twig template using assetic's {% stylesheets %} function with the less filter:
{% stylesheets "#AcmeTwitterBundle/Resources/bootstrap/less/*.less" filter="less" %}
{# ... {{ asset_url }} ...#}
{% endstylesheets %}
Now how to find out?
At first check your config files app/config/config.yml and other included ones for assetic entries using the less filter.
The easiest way to find out where the less filter is being used is installing ElaoWebProfilerExtraBundle, clearing your cache and having a look at the "Assetic" tab in the left-side menu of the profiler. You will get an overview of all assetic collections and the filters they use.
Another option - not involving a new bundle although WebProfilerExtraBundle is awesome - is to disable your third-party bundles one by one ( and clear the cache each time ) in app/AppKernel.php try if assetic:dump still throws the exception until you find the bad-boy.
Or dirty: enable the filter though less is probably not installed and see where the next exception is thrown:
assetic:
filters:
less: ~
... all in all i would bet you included the supercool mopa-bootstrap bundle of which almost nobody knows what it's actually doing behind the scenes but it's famous and many people install it because of it's KnpBundle's score.
meaning ... your exception would then be thrown because of the less files included in the templates provided by MopaBootstrapBundle i.e. here.

Resources