Symfony2: adding log in and log out option html symfony2 - php

I'm using Symfony2. I have a main twig template with a navbar where I want to have a pull-down menu with the options 'log in' or 'log out' depending on whether the user is logged in or not.
What is the best approach to accomplish this?
-By making a different, static main template for /admin/* with the log out option
-By checking whether the user is logged in or not inside each controller being called by the routing system and then passing the correct information to be shown to the twig template when it's time to render it
-By calling a specific controller like buildNavbarAuthOption() from inside the template
Last option doesn't seem the best one when trying to program using MVC, right?

Just use is_granted in your views:
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
Logout
{% else %}
Login
{% endif %}

In your main template you could test if the variable app.user exists like this:
{% if app.user %}
{# display logout #}
{% else %}
{# display login #}
{% endif %}
Or better yet, test if the current visitor has the minimum security role:
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}

Related

multisite eZ Platform installation

I'm building a multisite eZ Platforme installation and I need to specify a main layout for my templates.
Right now I have a template article.html.twig :
{% extends "main_layout.html.twig" %}
{% block content %}
<h1>{{ ez_render_field(content, 'body') }}</h1>
{% endblock %}
what I want to do is something like this :
{% if(siteaccess = "site1"){
extends "site1_main_layout.html.twig"
}
else if(siteaccess = "site2"){
extends "site1_main_layout.html.twig"
}
%}
Please help me!
You can just configure the layout in the config:
ezpublish:
system:
site1:
pagelayout: "tpl1.html.twig"
site2:
pagelayout: "tpl2.html.twig"
After that, you can just use the following in your full view:
{% extends pagelayout %}
{% block content %}
...
{% endblock %}
pagelayout is a variable prepopulated by eZ Platform from the above config based on current siteaccess. It requires eZ Platform 1.2 at the least, I believe.
It should also be noted that pagelayout variable is available only in full view templates. Other templates wishing to use the configured pagelayout must use the following:
{% extends ezpublish.configResolver.parameter('pagelayout') %}
Correct me if I misunderstood your goal, but do you recon it could be sorted by checking the domains? (I am assuming they would be different so could serve as a separator):
{% if app.request.baseUrl == 'site1' %}
...
{% else %}
...
{% endif %}
If I am not wrong, I also belive you can create a default Twig Controller Loader to decide this beforehand instead of leaving the logic to your views :)
Have a look here https://doc.ezplatform.com/en/latest/guide/design_engine/
you can use the Design engine where you can set up different themes with fallback.
Define one base theme an add per siteaccess one extra theme where you can override any template.

Inherit dynamic template in Phalcon Volt

I need to load a page, that will be "inserted" in a template - as I read it, Volt's Template Inheritance should do the trick and it does... kinda. Hardcoded values, as shown in the examples, work fine - the following example works:
<!-- Template -->
<div id="site_content">
{% block test %}
{% endblock %}
</div>
and the page, that inherits the template:
{% extends "../../templates/de/index.volt" %}
{% block test %}
{{ content() }} {# this is a registered volt function that outputs the generated content #}
{% endblock %}
However, the same page might need to inherit a different template and that must be decided on runtime, so the name of the template must be generated dynamically. Two options occurred to me:
Set the template name to a variable and use it when extending - the problem here is that I don't see a way to use it afterwards. That guy seems to have had the same problem, but there is neither an answer of how to do it, nor a confirmation that it isn't possible at all.
Register another function to generate the complete string (e.g. {% extends "../../templates/de/index.volt" %}) and then compile it, e.g.
$compiler->addFunction('get_template',
function ($resolvedArgs, $exprArgs) use ($volt) {
return $volt->getCompiler()
->compileString('{% extends "../../templates/de/index.volt" %}');
});
and then use that function in the page, e.g.
{{ get_template() }}
{% block test %}
{{ content() }}
{% endblock %}
However, using that approach does not parse the page content (e.g. the content returned by the registered content() function is not shown). I'm also open to other solutions (using Twig instead of Volt is only a last resort, for performance issues), advices of what I'm doing wrong or pointers of useful articles on the topic. Thanks in advance!
Try using partials as documented in the Phalcon doc: Using Partials

Symfony2 fos login, register and forgot password in one view

I need to apply a purchased template to our dashboard. In this template, the login, register and forgot password forms are all under the same view, and switching between them using simple JQuery.
I have been looking for a nice, not-too-flashy way of combining all three forms into one, but I came up empty.
My standing options (as I see them), and why I don't like any of them:
Take the views from the fos bundle, copy them to /app/Resources/FOSUserBundle/views/, remove the {% extend %} part and {% include %} them in my own login view. Reason for dislike: to me this looks a little like a quick-n-dirty fix - "that part's not working? Let's break it off!" :)
Extend the fos bundle, accept an extra parameter in the LoginAction and RegisterAction, use {% render %} with parameters in my own login view. Reason for dislike: extending a whole bundle and modifying two different controllers just to change the way it renders feels like bad MVC.
XHR load everything. Reason for dislike: this approach makes sense when using inner pages, but for pages that reload anyway it just doesn't make sense.
TL;DR version: I'm looking for a non-hack way of including the login, register and forgot password form in one page.
Any help would be greatly appreciated!
I found a solution with which I am comfortable with for my current project. The advantages and disadvantages of the proposed solution upfront:
Advantages:
few LOC to implement
FOSUserBundle update proof (does not override the view scripts*)
Disadvantages:
performance overhead due to subrequests
only forms can be displayed, form submission (and subsequently error handling upon submission) will always go to the pages provided by FOSUserBundle
still feels like a quick-n-dirty fix, but better than other options
* only needs to override the layout.html.twig file
With that being said, here is what I have done:
Render the form in your template
Use embedded controllers to render the forms you need:
<div>
<h2>Login</h2>
{{ render(controller('FOSUserBundle:Security:login', { embeddedForm: true})) }}
</div>
<div>
<h2>Reset</h2>
{{ render(controller('FOSUserBundle:Resetting:request', { embeddedForm: true})) }}
</div>
Override FOSUserBundle layout
As I use the routes provided by the bundle, I had to override the FOSUserBundle layout template file to extend the standard layout of my application. As the overriden FOSUserBundle layout file extends the main applications layout file the layout would be repeated for each call {{ render ... }}. To prevent that, we need to dynamically disarm the extended layout file. Here is what the overriden layout file looks like:
{# app/Resources/FOSUserBundle/views/layout.html.twig #}
{% if app.request.get('embeddedForm') %}
{% set layout = 'AcmeBundle::layout-content.html.twig' %}
{% else %}
{% set layout = 'AcmeBundle::layout.html.twig' %}
{% endif %}
{% extends layout %}
{% block content %}
{% block fos_user_content %}{% endblock %}
{% endblock %}
Create the AcmeBundle::layout-content.html.twig file
This layout should only render the content block of the FOSUserBundle view scripts and is such short and simple:
{# src/Acme/DemoBundle/Resources/views/layout-content.html.twig #}
{% block content %}{% endblock %}
Now the forms will render nicely with all dependencies (CSRF and so forth). Submitting the form will however take you to the FOSUserBundle actions.
Alternative solution:
This answer describes how to manually implement the forms and link them to the FOSUserBundle controller.

ezpublish/symfony renders 404 in Smarty, everything else in Twig

On my eZ publish 5 site I have all my templates in Twig, in the vendor/ezsystems/demobundle/EzSystems/DemoBundle/Resources/views/ subfolders. They are all being used throughout my whole site, no problems there. With one exception: 404 pages. If I go to mysite/nonexistingurl, it gives me a kernel (20) / Error page, with status 404. The template being used for this is the 20.tpl somewhere in eZ publish/symfony, I don't want that, I want to use my own Twig template for this.
How can I achieve this? I added a vendor/ezsystems/demobundle/EzSystems/DemoBundle/Resources/views/Exception/error.html.twig page, but this one is not being called
first add this configuration parameter
parameters:
ezpublish_legacy.default.module_default_layout: 'YourBundle::pagelayout_legacy.html.twig'
you may add it in the parameters.yml file located in path/to/yourezpublishinstall/ezpublish/config, the parameters.yml is usually imported in the config.yml located in the same folder
this would define the twig template located in path/to/yourbundle/Resources/views/pagelayout_legacy.html.twig as the parent template for legacy stack modules templates
inside the pagelayout_legacy.html.twig template, you may use this code
{% extends 'YourBundle::pagelayout.html.twig' %}
{% block content %}
{# module_result variable is received from the legacy controller. #}
{% if module_result.errorCode is defined %}
<h1>{{ module_result.errorMessage }} ({{ module_result.errorCode }})</h1>
{% else %}
{{ module_result.content|raw }}
{% endif %}
{% endblock %}
note in the code, the template extends the pagelayout.html.twig template, that should here define a block named content, the pagelayout.html.twig may usually be the main base layout for your ez publish 5 website
you may modify the pagelayout_legacy.html.twig template to your needs
reference:
http://share.ez.no/forums/developer/overriding-legacy-error-pages-templates

FOSUserBundle , dynamically change twig to render

In order to override a template for FOSUserBundle, I should create a twig file with the same name conserving the hierarchy. That's fine and it works perfectly.
My problem is that the twig file is not static ( in the desktop version I will render a twig file and in the mobile version we will render another one). I decide which twig to render on the controller by testing on a session variable.
Is there a solution to dynamically change the twig to render in FOSUserBundle without overriding all controllers?
You can test your session variable in twig also. Simply include another template in your twig :
{% if app.session.isMobile %}
{% include '::mobile.html.twig' %}
{% else %}
{% include '::desktop.html.twig' %}
{% endif %}

Categories