Setting up PhpStorm with Symfony's service container?
Hi all,
I'd like to get autocomplete for variables using S2's service container:
if ($form->isValid()) {
$message = new \Swift_Message();
$message->setSubject('Contact enquiry from symblog')
->setFrom('enquiries@symblog.co.uk')
->setTo($this->container->getParameter('blogger_blog.emails.contact_email'))
->setBody($this->renderView(
'BloggerBlogBundle:Page:contactEmail.txt.twig',
array('enquiry' => $enquiry)));
$mailer = $this->get('mailer');
$mailer->send($message);
$this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');
// Redirect - This is important to prevent users re-posting
// the form if they refresh the page
return $this->redirect($this->generateUrl('BloggerBlogBundle_contact'));
}
These parts:
$mailer = $this->get('mailer');
$mailer->send($message);
$this->get('session')->setFlash('blogger-notice', 'Your contact enquiry was successfully sent. Thank you!');
both use the container, but send() and setFlash() aren't detected. How would I go about making this work seamlessly?
Thanks!
请先登录再写评论。
Hi Juan,
Provide a type hint via PHPDoc comment:
Obviously, replace MyMailerClass by correct class name (sorry, I'm not Symfony user, so don't know exact class name)
Hi Andriy,
The problem with this solution is that it defeats the purpose of a dependency injection container - you're tying the IDE in to a specific class, instead of allowing the DIC to set the class you want.
Hi Juan,
Until IDE will know how to calculate/predict not-fixed/dynamically-generated return type of $this->get('mailer') you have no other choice if you want to have code completion working.
P.S.
If tomorrow $this->get('mailer')will return MySuperMailerClass instead of current MyMailerClass, what difference it will make for already existing code (considering that you will not edit the code)?