Skip to content

Instantly share code, notes, and snippets.

@rei999
Last active August 29, 2015 14:17
Show Gist options
  • Save rei999/69476ca0c7b3aa085b02 to your computer and use it in GitHub Desktop.
Save rei999/69476ca0c7b3aa085b02 to your computer and use it in GitHub Desktop.
Cheat Sheet for Symfony 2.6
Create a new project:
symfony new {{project_name}}
php app/console generate:bundle --namespace=Acme/DemoBundle --format=yml
Help:
php app/console config:dump-reference FrameworkBundle
Doctrine:
php app/console doctrine:database:create
php app/console doctrine:generate:entities TwoSixRandomBundle:Product
php app/console doctrine:schema:update --force
Important commands:
php app/console cache:clear --env=prod
Start symfony server:
php app/console server:run
Twig - Embed a controller:
{{ render(controller('AppBundle:Default:topArticles')) }}
Link to assets:
<link href="{{ asset('css/blog.css') }}" rel="stylesheet" type="text/css" />
Using route annotation:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Route("/hello/{name}", name="hello")
*/
Errors:
404 - throw $this->createNotFoundException();
500 - throw new \Exception('Something went horribly wrong!');
Request Parameters:
$pageName = $request->query->get('page'); // GET
$pageName = $request->request->get('page'); // POST
{{ app.request.query.get('page') }}
$request->server->get('HTTP_HOST');
$request->files->get('foo');
$request->cookies->get('PHPSESSID');
$request->headers->get('content_type');
Session:
$session = $request->getSession();
$session->set('foo', 'bar');
$foo = $session->get('foo', 'bar'); // 2nd arg is default value
Flash:
$this->addFlash('notice', 'Congratulations, your action succeeded!');
<div>
{{ app.session.flashbag.get('notice') }}
</div>
{% extends "layout.html.twig" %}
{% block title %}List of Posts{% endblock %}
{% block body %}
<h1>List of Posts</h1>
<ul>
{% for post in posts %}
<li>
<a href="{{ path('blog_show', {'id': post.id}) }}">
{{ post.title }}
</a>
</li>
{% endfor %}
</ul>
{% endblock %}
Routing:
view_study_class:
pattern: /courses/{id}/flashcards/{page}
defaults: { _controller: MoonLightMainBundle:StudyClass:viewStudyClass, page: 0 }
Parameters:
Doctrine insert:
$product = new Product();
$product->setName('A Foo Bar');
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
Doctrine remove:
$em->remove($product);
$em->flush();
Doctrine get:
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Product p
WHERE p.price > :price
ORDER BY p.price ASC'
)->setParameter('price', '19.99');
$query->getOneOrNullResult();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment