Autor wpisu: Kamil Adryjanek, dodany: 06.07.2012 19:46, tagi: symfony2, eclipse
Today I want to share with you another great plugin that is perfect for Symfony2 developers or anyone who wants to have Doctrine, Twig or Yaml support using Eclipse IDE.
From symfony.dubture.com:
What’s this?
An Eclipse plugin for Symfony and the Symfony components . Features include:
– Codeassist for Symfony specific elements, like services, routes, template paths, entities, translations and twig blocks. – Navigation: Hyperlinking of routes, templates, twig blocks/functions/filters and services – Annotation support – Twig support Twig
Twig is a templating language for PHP. Symfony has built in support for Twig, and so does the Symfony Eclipse Plugin. Doctrine
Doctrine is a
viagra onlineset of PHP libraries primarily focused on providing persistence services and related functionality. It comes bundled with the Symfony Standard-Edition as the default ORM. The Symfony Eclipse Plugin also provides Doctrine support.
Yaml
Yaml is a data serialization standard which is supported by Symfony. If you’re using yaml, you can use the optional Yedit feature. The plugin is maintained by oyse. Too keep things simple, the Symfony Plugin updatesite makes the Yedit feature available too.
Autor wpisu: Wojciech Sznapka, dodany: 20.04.2012 19:49, tagi: symfony2, php
Autor wpisu: Tomasz Kowalczyk, dodany: 11.04.2012 22:28, tagi: symfony2, php, framework, design
Autor wpisu: Tomasz Kowalczyk, dodany: 26.03.2012 22:09, tagi: symfony2, php
Autor wpisu: Wojciech Sznapka, dodany: 18.01.2012 21:05, tagi: php, symfony2
Autor wpisu: Kamil Adryjanek, dodany: 13.11.2011 02:04, tagi: symfony2, php
In one of my templates i needed a simple way to get controller / action name to generate some dynamic urls. Symfony2 does not offer any Twig helper function to display current controller / action name.
The easiest way that i have found so far is to create Twig extension. In our default bundle we need to create folder Twig/Extension for example Acme/PageBundle/Twig/Extension and place there our Twig extension class:
<?php // src/Acme/PageBundle/Twig/Extension/AcmePageExtension.php namespace Acme\PageBundle\Twig\Extension; use Symfony\Component\HttpFoundation\Request; class AcmePageExtension extends \Twig_Extension { protected $request; /** * * @var \Twig_Environment */ protected $environment; public function __construct(Request $request) { $this->request = $request; } public function initRuntime(\Twig_Environment $environment) { $this->environment = $environment; } public function getFunctions() { return array( 'get_controller_name' => new \Twig_Function_Method($this, 'getControllerName'), 'get_action_name' => new \Twig_Function_Method($this, 'getActionName'), ); } /** * Get current controller name */ public function getControllerName() { $pattern = "#Controller\\\([a-zA-Z]*)Controller#"; $matches = array(); preg_match($pattern, $this->request->get('_controller'), $matches); return strtolower($matches[1]); } /** * Get current action name */ public function getActionName() { $pattern = "#::([a-zA-Z]*)Action#"; $matches = array(); preg_match($pattern, $this->request->get('_controller'), $matches); return $matches[1]; } public function getName() { return 'acme_page'; } }
Next step is to register this service:
// src/Acme/PageBundle/Resources/config/services.yml request: class: Symfony\Component\HttpFoundation\Reques acme.twig.extension: class: Acme\PageBundle\Twig\Extension\AcmePageExtension arguments: [@request] tags: - { name: 'twig.extension' }
and then in twig templates we can simply call:
Controller name: {{ get_controller_name() }} Action name: {{ get_action_name() }}