We all read the best practices published a while ago, didn't we? In this talk, we will give some more insights into additional practices that you might consider in your next Symfony project.
3. About Me
• Developer and trainer at King Foo
• SensioLabs Certified Symfony Developer
• We develop tailor-made PHP applications
• @gerryvdm / gerry@king-foo.be
4. The Official Best Practices
• Excellent guidelines for beginning Symfony
developers
• Use a single bundle for application specific
code
• Use the app/Resources/views/ directory for
all application specific templates
• Use YAML for application configuration
29. use SymfonyComponentHttpKernelEventGetResponseEvent;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
class CheckLocaleListener
{
private $availableLocales;
public function __construct(array $availableLocales)
{
$this->availableLocales = $availableLocales;
}
public function checkLocale(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($locale = $request->attributes->get('_locale')) {
if (!in_array($locale, $this->availableLocales)) {
throw new NotFoundHttpException();
}
}
}
}
36. class DependentService extends ContainerAware
{
public function doSomething()
{
$this->container->get('my_fantastic_service')
->doSomethingElse();
}
}
37. class DependentService
{
public function __construct(FantasticService $service)
{
$this->service = $service;
}
public function doSomething()
{
$this->service->doSomethingElse();
}
}
49. class BadController
{
public function __invoke()
{
$template = $_GET['template'];
setcookie('lastGeneration', time());
$this->archaicPdfLib->output($template);
exit;
}
}
50. use SymfonyComponentHttpFoundationCookie;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationStreamedResponse;
class BetterController
{
public function __invoke(Request $request)
{
$template = $request->query->get('template');
$response = new StreamedResponse(
function() use ($template) {
$this->archaicPdfLib->output($template);
}
);
$cookie = new Cookie('lastGeneration', time());
$response->headers->setCookie($cookie);
return $response;
}
}
62. Miscellaneous / Flamebait
• Bundles are for extending the container and
configuring (third-party) libraries that are
reusable across projects in the container.
• Try doing without bundles for project-
specific configuration.
• YAML is the preferred configuration file
format for application-specific config, XML
for configuration of bundles
63. Develop Your Own
Best Practices
• Do it as a team
• Write them down
• Be consistent
• Learn regex 😉
64. Next Symfony Training
• Getting Started with Symfony2
• 12-13th February 2015
• Aarschot
• Leave your name + email for a €50 reduction