为了本示例创建一个名为ExtendedMailer的新类,并将该文件保存在自动加载器能够找到的地方。根据您放置文件的位置,您可能需要在保存文件后运行composer dump-autoload
。
<?php
use Illuminate\Mail\Mailer;
class ExtendedMailer extends Mailer
{
protected function logMessage($message)
{
parent::logMessage($message);
$emails = implode(', ', array_keys((array) $message->getCc()));
$this->logger->info("Pretending to mail message to: {$emails}");
}
}
创建一个新的服务提供者,在您的应用程序可以加载类的地方。如上所述,您可能需要运行composer dump-autoload
下面的代码只是扩展了原始的MailServiceProvider,但允许我们将不同的类绑定到IoC中,您会注意到new ExtendedMailer
;我们之前创建的类。显然,如果你为班级命名,在这里反映这种变化。
<?php
use Illuminate\Mail\MailServiceProvider;
class ExtendedMailServiceProvider extends MailServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$me = $this;
$this->app->bindShared('mailer', function($app) use ($me)
{
$me->registerSwiftMailer();
// Once we have create the mailer instance, we will set a container instance
// on the mailer. This allows us to resolve mailer classes via containers
// for maximum testability on said classes instead of passing Closures.
$mailer = new ExtendedMailer(
$app['view'], $app['swift.mailer'], $app['events']
);
$this->setMailerDependencies($mailer, $app);
// If a "from" address is set, we will set it on the mailer so that all mail
// messages sent by the applications will utilize the same "from" address
// on each one, which makes the developer's life a lot more convenient.
$from = $app['config']['mail.from'];
if (is_array($from) && isset($from['address']))
{
$mailer->alwaysFrom($from['address'], $from['name']);
}
// Here we will determine if the mailer should be in "pretend" mode for this
// environment, which will simply write out e-mail to the logs instead of
// sending it over the web, which is useful for local dev environments.
$pretend = $app['config']->get('mail.pretend', false);
$mailer->pretend($pretend);
return $mailer;
});
}
}
在你的config/app.php,你会发现这样一行看起来像
'Illuminate\Mail\MailServiceProvider',
你需要把它注释掉,并添加一行如下
'ExtendedMailServiceProvider',
这是做什么的,取代Laravel知道你刚刚创建的邮件程序。你刚创建的那个和默认的一样,因为它只是扩展它,并且增加了logMessage函数的功能。
感谢您的回复。但我只需要在邮件配置中使用假装值。 – 2014-11-23 12:25:26
你的意思是你需要找到插入到加密中的值/函数,对吧? – 2014-11-23 13:38:13
我用linke更新我的问题。请检查一下。 – 2014-11-25 11:20:45