// src/AppBundle/Controller/HelloController.phpnamespace AppBundle\Controller;use Symfony\Component\HttpFoundation\Response;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;class HelloController{ /** * @Route("/hello/{name}", name="hello") */ public function indexAction($name) { return new Response('Hello '.$name.'!'); }}
// src/AppBundle/Controller/HelloController.phpnamespace AppBundle\Controller;use Symfony\Component\HttpFoundation\Response;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;class HelloController{ /** * @Route("/hello/{name}", name="hello") */ public function indexAction($name) { $text = "Name is ".$name; return $text; }}
// src/AppBundle/Controller/HelloController.php// ...use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;class HelloController{ /** * @Route("/hello/{firstName}/{lastName}", name="hello") */ public function indexAction($lastName, $firstName) { return new Response('Hello '.$firstName.', '.$lastName.'!'); }}
// src/AppBundle/Controller/HelloController.php// ...use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;class HelloController{ /** * @Route("/hello/{firstName}/{lastName}", name="hello") */ public function indexAction($lastName, $firstName, $company) { return new Response('Hello '.$firstName.', '.$lastName.'!'); }}
// src/AppBundle/Controller/HelloController.php// ...use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;class HelloController{ /** * @Route("/hello/{firstName}/{lastName}", name="hello") */ public function indexAction($lastName) { return new Response('Hello '.$lastName.'!');}}
// src/AppBundle/Controller/HelloController.php// ...use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;class HelloController{ /** * @Route("/hello/{firstName}", name="hello") */ public function indexAction($firstName, Request $request) { $lastName = $request->request->get('lastName'); return new Response('Hello '.$firstName.', '.$lastName.'!');}}
namespace AppBundle\Controller;use Symfony\Bundle\FrameworkBundle\Controller\Controller;class HelloController{ public function indexAction() { return $this->render('template.html.twig'); }}
namespace AppBundle\Controller;use Symfony\Bundle\FrameworkBundle\Controller\Controller;class HelloController extends Controller{ public function indexAction() { return $this->render('template.html.twig'); }}
namespace AppBundle\Controller;use Symfony\Bundle\FrameworkBundle\Controller\Controller;class HelloController extends Controller{ public function indexAction() { return $this->redirect($this->generateUrl('homepage')); }}
$request->getSession()->getFlashBag()->add( 'terrible_notice', 'You ran out of milk!');
$response = new Response(json_encode(array('name' => $name)));$response->headers->set('Content-Type', 'application/json');
public function indexAction($name){ $response = $this->forward('AppBundle:Something:fancy', array( 'name' => $name, 'color' => 'green',)); // ... further modify the response or return it directly return $response;}
# app/config/services.ymlservices: app.hello_controller: class: AppBundle\Controller\HelloController
// .../** * @Route("/blog/{slug}", name="blog_show") */public function showAction($slug){ // ...}
/** * @Route("/blog/{page}", defaults={"page" = 1}) */public function indexAction($page = 2){// ...}
// src/AppBundle/Controller/BlogController.php// ...class BlogController extends Controller{ /** * @Route("/blog/{page}", defaults={"page" = 1}) */ public function indexAction($page) { // ... } /** * @Route("/blog/{slug}") */ public function showAction($slug) { // .. }}
// src/AppBundle/Controller/MainController.php// ...class MainController extends Controller{ /** * @Route("/{_locale}", defaults={"_locale": "en"}, requirements={ * "_locale": "en|fr" * }) */ public function homepageAction($_locale) { }}
// src/AppBundle/Controller/MainController.phpnamespace AppBundle\Controller;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;// ...class MainController extends Controller{ /** * @Route("/news") * @Method("GET") */ public function newsGetAction() { // ... display your news} /** * @Route("/news") * @Method({"POST"}) */ public function newsPostAction() { // ... display and process a contact form }}
homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage }mobile_homepage: path: / host: "{subdomain}.example.com" defaults: _controller: AcmeDemoBundle:Main:mobileHomepage subdomain: m requirements: subdomain: m|mobile
// src/AppBundle/Controller/ArticleController.php// ...class ArticleController extends Controller{ /** * @Route( * "/articles/{_locale}/{year}/{title}.{_format}", * defaults={"_format": "html"}, * name = 'app_news', * requirements={ * "_locale": "en|fr", * "_format": "html|rss", * "year": "\d+" * } * ) */ public function showAction($_locale, $year, $title, $_route = 'my_route', $_controller) { }}
/**/* @Route("/blog/{slug}", name="blog_post")*/public function showAction($slug){$url = $this->get('router')->generate('blog_post', array( 'slug' => 'el-quijote', 'page' => 2));}
/**/* @Route("/blog/{slug}", defaults = {'slug' : 'hamlet' }, name="blog_post")*/public function showAction($slug){$url = $this->get('router')->generate('blog_post', array( 'slug' => 'el-quijote'), true);}
namespace AppBundle\Controller;class BlogController extends Controller{/**/* @Route("/blog/{slug}", defaults = {'slug' : 'hamlet' }, name="blog_post")*/public function showAction($slug){ $var = $this->get('router')->match('/blog/my-blog-post');}}
namespace AppBundle\Controller;class BlogController extends Controller{/**/* @Route("/blog/{slug}", defaults = {'slug' : 'hamlet' }, name="blog_post")*/public function showAction($slug){ $var = $this->get('router')->match('/blog');}}
namespace AppBundle\Controller;class BlogController extends Controller{/**/* @Route("/blog/{slug}", name="blog_post")*/public function showAction($slug){ // ...}}
{% for item in navigation if navigation is defined %} {% endfor %}
Template 1 -{# src/AppBundle/Resources/views/base.html.twig #} {% block title %}Test Application{% endblock %} {% block sidebar %} Home Blog {% endblock %} {% block body %}{% endblock %} Template 2 -{# src/AppBundle/Resources/views/blog/index.html.twig #}{% extends 'AppBundle::base.html.twig' %}{% block title %}My cool blog posts | {{ parent() }}{% endblock %}
Template 1 -{# src/AppBundle/Resources/views/base.html.twig #} {% block title %}Test Application{% endblock %} {% block sidebar %} Home Blog {% endblock %} {% block body %}{% endblock %} Template 2 -{# src/AppBundle/Resources/views/blog/index.html.twig #}{% extends '@AppBundle/base.html.twig' %}{% block title %}My cool blog posts{% endblock %}
Template 1 -{# app/Resources/AppBundle/views/index.html.twig #} {% block title %}Test Application{% endblock %} {% block body %} Hello! {% endblock %} Template 2 -{# src/AppBundle/Resources/views/index.html.twig #} {% block title %}Test Application{% endblock %} {% block body %} Hello! {% endblock %}
{# src/AppBundle/Resources/views/index.html.json #} {% block title %}Test Application{% endblock %} {% block body %} Hello! {% endblock %}
Template 1 -{# app/Resources/views/article/list.html.twig #}{% extends 'layout.html.twig' %}{% block body %} Recent Articles {% for article in articles %} {{ include('article/article_details.html.twig') }} {% endfor %}{% endblock %}Template 2 -{# app/Resources/views/article/article_details.html.twig #}{{ article.title }} by {{ article.authorName }} {{ article.body }}
{{ article.body }}
{% block stylesheets %} {% stylesheets '@FOSCommentBundle/Resources/assets/css/comments.css' '/media/css/all.css' '/custom/css/*'%} {% endstylesheets %}{% endblock %}
# app/config/config.ymlassetic: filters: coffee: bin: /usr/bin/coffee node: /usr/bin/node node_paths: [/usr/lib/node_modules/] apply_to: "\.coffee$"
# app/config/config.ymlframework: # ... templating: engines: ['twig', 'php']
Hello {{ name }}
# Twig Configurationtwig: strict_variables: "%kernel.debug%"
{# app/Resources/views/article/recent_list.html.twig #}{{ dump(articles) }}{% for article in articles %} {{ article.title }} {% endfor %}
//src/AppBundle/Twig/SayYeahExtension.phpnamespace AppBundle\Twig;class SayYeahExtension extends \Twig_Extension{ public function getFilters() { return array( new \Twig_SimpleFilter('say_yeah', array($this, 'sayIt')) ); } public function sayIt($text) { return $text." Yeah!"; } public function getName() { return "sayYeah"; }}\# app/config/services.ymlservices: say_whatever.gromenaguer: class: AppBundle\Twig\SayYeahExtension public: false tags: - { name: twig.extension }
//src/AppBundle/Tests/Utils/SyllableSeparatorTest.phpnamespace AppBundle\Tests\Utils;use AppBundle\Utils\SyllableSeparator;class SyllableSeparatorTest { public function testTildes() { $tests = array ('bíceps' => 1, 'imágen' => 0, 'ataúd' => 1, 'estáis' => 1); $i = 0; foreach ($tests as $word => $result) { $analyzer = new SyllableSeparator($word); $this->assertEquals($result, $analyzer->llevaTilde()); $analyzer = null; $i++; } }}
//src/AppBundle/Tests/Utils/SyllableSeparatorTest.phpnamespace AppBundle\Tests\Controller;use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;class DefaultControllerTest extends WebTestCase{ public function testIndex() { $client = static::createClient(); $crawler = $client->request('GET', '/'); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertContains('Lleva tilde esta palabra', $crawler->filter('div.container h1')->text()); }}
$client = static::createClient();
$client = static::createClient();$crawler = $client->request('GET', '/post/hello-world');
$link = $crawler->filter('a:contains("Greet")');
$this->assertContains( 'Hello World', $client->getResponse()->getContent() );
$crawler->attr('class');
$info = $crawler->extract(array('_text', 'href'));
$data = $crawler->each(function ($node, $i) { return $node->attr('href');});
$link = $crawler->selectLink('Click here');$client->click($link);
$buttonCrawlerNode = $crawler->selectButton('submit');$form = $buttonCrawlerNode->form(array( 'name' => 'Frank', ), POST);$client->submit($form, array( 'name' => 'Peter' ));