Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / ArticleController.php
Created May 2, 2012 11:56
Some Symfony controller code rendering a blog article using a Twig template - http://www.symfonycentral.com
<?php
# src/MyCompany/BlogBundle/Controller/ArticleController.php
namespace MyCompany\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ArticleController extends Controller
{
public function viewAction($id)
{
@coreymcmahon
coreymcmahon / index.html.twig
Created May 2, 2012 12:16
Bigger example of a Twig template - http://www.symfonycentral.com
<!DOCTYPE html>
<html>
<head>
{# title has been passed from the controller as an argument to 'render' #}
<title>{{ title|raw }}</title>
</head>
<body>
<nav>
<ul id="navigation">
{# Here navigation is an array of arrays... #}
{# app/MyCompany/BlogBundle/Resources/views/layout.html.twig #}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<div id="content">{% block content %}{% endblock %}</div>
</body>
</html>
@coreymcmahon
coreymcmahon / view.html.twig
Created May 2, 2012 12:38
Twig template that inherits from another within the Bundle - http://www.symfonycentral.com
{% extends 'MyCompanyBlogBundle::layout.html.twig' %}
{% block title %}{{ article.title }}{% endblock %}
{% block content %}
{{ article.body }}
{% endblock %}
{# In the parent template #}
{% for item in navigation %}
{% include "navigation.html.twig" %}
{% endfor %}
{# In 'navigation.html.twig' #}
<a href="{{ item.href }}">{{ item.title }}</a>
@coreymcmahon
coreymcmahon / ArticleController.php
Created May 2, 2012 13:03
A Symfony Controller using the @template annotation - http://www.symfonycentral.com
<?php
# src/MyCompany/BlogBundle/Controller/ArticleController.php
namespace MyCompany\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ArticleController extends Controller
{
/**
* @Route("/article/{id}", name="_view_article")
@coreymcmahon
coreymcmahon / Article.php
Created May 3, 2012 10:27
An entity created by the Doctrine 2 ORM - http://www.symfonycentral.com
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Acme\BlogBundle\Entity\Article
*
* @ORM\Table()
* @ORM\Entity
@coreymcmahon
coreymcmahon / DefaultController.php
Created May 3, 2012 12:14
A very simple Symfony controller demonstrating the use of the Doctrine ORM - http://www.symfonycentral.com
<?php
namespace MyCompany\BlogBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/* Note below: we NEEED to include the Entity class Doctrine built for us */
use MyCompany\BlogBundle\Entity\Post as Post;
@coreymcmahon
coreymcmahon / index.html.twig
Created May 3, 2012 12:15
Twig template for viewing blog posts - http://www.symfonycentral.com
{% if title == '' %}
{#
If the title is empty, the controller didn't pass us
what we were expecting :(
#}
Please provide a valid ID
{% else %}
<h1>{{ title|raw }}</h1>
<p>{{ body|raw }}</p>
{% endif %}
{#
This is a VERY basic form with two fields, which we'll
use to create the object to insert into the database.
#}
<form action="" method="POST">
{#
Note: we normally wouldn't construct a form like this in
Symfony, as Symfony provides us with a WHOLE framework
dedicated to forms.