B R*_*Rad 2 php controller symfony
我试图用Swift_Message发送邮件但是当我去发送它不会发送的数据时我得到一个错误
FatalErrorException:错误:在/vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php第252行中调用非对象上的成员函数get()
这是我正在使用的电子邮件控制器.
use Symfony\Component\Finder\Shell\Command;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EmailController extends Controller{
public function createMessage($subject, $from, $from_name, $to, $to_name, $body){
// Create the message
$message = \Swift_Message::newInstance()
// Give the message a subject
->setSubject($subject)
// Set the From address with an associative array
->setFrom(array($from => $from_name))
// Set the To addresses with an associative array
->setTo(array($to => $to_name))
// Give it a body
->setBody($body, 'text/html');
return $message;
}
public function sendEmail($message, $urlAlias){
$this->get('mailer')->send($message);
return $this->redirect($this->generateUrl($urlAlias));
}
}
Run Code Online (Sandbox Code Playgroud)
我知道它无法访问我认为是容器类的一部分的对象,但我似乎可以让它拉起来.我试过用$this->container->get(...
但这也行不通.我错过了什么 这似乎应该是非常直接的.
我使用动作调用当前控制器从另一个包中调用此函数.我不知道这是否有所作为.
好的,所以当查看/vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php时
它错误的行是
/**
* Gets a service by id.
*
* @param string $id The service id
*
* @return object The service
*/
public function get($id)
{
return $this->container->get($id);
}
}
Run Code Online (Sandbox Code Playgroud)
这让我感觉像'邮件; 不是一个好的$ id,但它在Symfony的例子和许多其他私人例子中使用.
不知道这是否有帮助,但认为值得一提.
这可能是因为swiftmailer:在我的config.yml文件中设置?
routing.yml文件
fuel_form_homepage:
pattern: /hello/{name}
defaults: { _controller: FuelFormBundle:Default:index }
referral_form:
pattern: /form/referral/{hash}
defaults: { _controller: FuelFormBundle:Form:referralForm }
referral_result:
pattern: /form/referral/result
defaults: { _controller: FuelFormBundle:Form:referralResult }
user_form:
pattern: /form/user
defaults: { _controller: FuelFormBundle:Form:userForm }
home:
pattern: /
defaults: { _controller: FuelFormBundle:Default:home}
Run Code Online (Sandbox Code Playgroud)
这是调用的函数
public function userFormAction(request $request){
$user = new User();
$form = $this->createForm('user', $user);
$form->handleRequest($request);
if($form->isValid()){
$user->setTimeCreated();
$user->setTimeUpdated();
$date = $user->getTimeCreated();
$timestamp = $date->format("U");
$hash = $user->getFirstName() . $user->getLastName() . $timestamp ;
$user->setUserHash(md5($hash));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
print_r($user);
//TODO: @Email: @Body: make sure to replace with correct information.
//Calls a service named email_bundle_controller
$emailController = $this->get('email_bundle_controller');
$fullName = $user->getFirstName() . $user->getLastName();
$body = "please visit the following url to start referring! <a href='http://localhost:8080/app_dev.php/form/referral/" . $user->getUserHash() . "'>Your URL</a>";
$message = $emailController->createMessage('Welcome to Fuel PRM References', 'bsaverino@gmail.com', 'Brad Saverino', $user->getEmail(), $fullName, $body);
$emailController->sendEmail($message, 'user_form');
}
return $this->render('FuelFormBundle:Default:mainForm.html.twig', array('form' => $form->createView(),));
}
Run Code Online (Sandbox Code Playgroud)
这是允许我调用另一个包的服务.
services:
fuel_form.form.type.referral:
class: Fuel\FormBundle\Form\Type\ReferralType
tags:
- { name: form.type, alias: referral}
fuel_form.form.type.user:
class: Fuel\FormBundle\Form\Type\UserType
tags:
- { name: form.type, alias: user}
email_bundle_controller:
class: Fuel\EmailBundle\Controller\EmailController
Run Code Online (Sandbox Code Playgroud)
这是FuelEmailBundle.php
namespace Fuel\EmailBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use \Symfony\Component\DependencyInjection\ContainerInterface;
class FuelEmailBundle extends Bundle
{
private static $containerInstance = null;
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer($container);
self::$containerInstance = $container;
}
public static function getContainer()
{
return self::$containerInstance;
}
}
Run Code Online (Sandbox Code Playgroud)
这些是对sendEmail函数所做的更改
public function sendEmail($message, $urlAlias){
$container = FuelEmailBundle::getContainer();
$mailer = $container->get('mailer');
$mailer->send($message);
return $this->redirect($this->generateUrl($urlAlias));
}
Run Code Online (Sandbox Code Playgroud)
正如Cerad上面提到的那样,由于未设置容器,因此收到错误.解决此问题的一种方法是将容器实例传递给您的包,以便您可以从项目的任何位置调用容器.
编辑与您的bundle(BundleName.php)对应的类,以包含两个方法setContainer和getContainer.请参阅下面的示例.
namespace Venom\CoreBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use \Symfony\Component\DependencyInjection\ContainerInterface;
class VenomCoreBundle extends Bundle
{
private static $containerInstance = null;
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer($container);
self::$containerInstance = $container;
}
public static function getContainer()
{
return self::$containerInstance;
}
}
Run Code Online (Sandbox Code Playgroud)
使用适当的命名空间.然后,在需要容器的类中使用bundle的命名空间.你可以通过调用容器
$container = VenomCoreBundle::getContainer();
然后,打电话给邮件
$mailer = $container->get('mailer');
归档时间: |
|
查看次数: |
9955 次 |
最近记录: |