Skip to content

Instantly share code, notes, and snippets.

@serapheem
serapheem / gist:6047530
Created July 21, 2013 04:55
Symfony2 - Include template inside another template
Twig:
{# src/Acme/ArticleBundle/Resources/Article/list.html.twig #}
{% extends 'AcmeArticleBundle::layout.html.twig' %}
{% block body %}
<h1>Recent Articles<h1>
{% for article in articles %}
{% include 'AcmeArticleBundle:Article:articleDetails.html.twig' with {'article': article} %}
{% endfor %}
@serapheem
serapheem / gist:6047528
Created July 21, 2013 04:54
Symfony2 - Global template variables
List:
app.security - The security context.
app.user - The current user object.
app.request - The request object.
app.session - The session object.
app.environment - The current environment (dev, prod, etc).
app.debug - True if in debug mode. False otherwise.
Twig:
@serapheem
serapheem / gist:6047527
Created July 21, 2013 04:53
Symfony2 - Getting Flash message in template
Twig:
{% if app.session.hasFlash('notice') %}
<div class="flash-notice">
{{ app.session.flash('notice') }}
</div>
{% endif %}
PHP:
<?php if ($view['session']->hasFlash('notice')): ?>
<div class="flash-notice">
@serapheem
serapheem / gist:6047525
Created July 21, 2013 04:52
Symfony2 - Generating links in template
Twig:
<a href="{{ path('_welcome') }}">Home</a>
PHP:
<a href="<?php echo $view['router']->generate('_welcome') ?>">Home</a>
If more complicated route.
Twig:
@serapheem
serapheem / gist:6047518
Last active December 20, 2015 01:09
Symfony2 - Useful console commands
# list of available commands
php app/console list
# getting list of routes
php app/console router:debug
# getting list of services
php app/console container:debug
# regenerating assets dump file
@serapheem
serapheem / gist:6047511
Created July 21, 2013 04:44
Symony2 - Redirecting to homepage in controller
<?php
return $this->redirect($this->generateUrl('homepage'));
@serapheem
serapheem / gist:6047509
Created July 21, 2013 04:43
Symfony2 - Getting services from controller
<?php
$request = $this->getRequest();
$templating = $this->get('templating');
$router = $this->get('router');
$mailer = $this->get('mailer');
@serapheem
serapheem / gist:6047504
Created July 21, 2013 04:41
Symfony2 - Generating and matching routes
<?php
$params = $router->match('/blog/my-blog-post');
// array('slug' => 'my-blog-post', '_controller' => 'AcmeBlogBundle:Blog:show')
$uri = $router->generate('blog_show', array('slug' => 'my-blog-post'));
// /blog/my-blog-post
?>
If the frontend of your application uses AJAX requests,
@serapheem
serapheem / gist:6047497
Created July 21, 2013 04:39
Symfony2 - Forwarding to another controller method
<?php
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
'name' => $name,
'color' => 'green'
));
@serapheem
serapheem / gist:6047491
Last active December 20, 2015 01:09
Symfony2 - Getting Base and Root Paths
<?php
// Sometimes you want to be able to get the base path url for your project
// and also another times you just want to get the root folder where
// the symfony2 application is living.
// For the first one, the url base path you get it this way:
$this->get('request')->getBasePath();