Laravels의 Mail의 클래스 facade 메소드를 무시하고 싶습니다. 일부 검사를 강제로 차단 한 다음 parent :: send()를 전달하는 경우Laravel Facade 메서드를 어떻게 재정의 할 수 있습니까?
가장 적합한 방법은 무엇입니까?
Laravels의 Mail의 클래스 facade 메소드를 무시하고 싶습니다. 일부 검사를 강제로 차단 한 다음 parent :: send()를 전달하는 경우Laravel Facade 메서드를 어떻게 재정의 할 수 있습니까?
가장 적합한 방법은 무엇입니까?
외관이 그렇게 작동하지 않습니다. 이것은 기본적으로 그것이 나타내는 기본 클래스를 호출하는 래퍼 클래스와 비슷합니다.
Mail
외관에는 실제로 send
방법이 없습니다. Mail::send()
을 수행하면 IoC 컨테이너에서 Illuminate\Mail\Mailer
클래스 바인딩의 인스턴스를 참조하는 데 "facade 접근 자"가 사용됩니다. 그 객체에 send
메서드가 호출됩니다.
당신이 원하는 것을 달성 할 수있는 방법은 실제로 보이는 것보다 조금 더 까다 롭습니다. 당신이 할 수있는 것은 :
send
메소드를 오버라이드 (override) 할 수있는 Illuminate\Mail\Mailer
을 확장, Mailer
당신 자신의 구현을 작성 수표를 구현하고 parent::send()
를 호출합니다.Illuminate\Mail\MailServiceProvider
연장)를 작성하고 특히 register
메소드를 다시 구현하십시오. Laravel 자신의 인스턴스 대신 자신의 Mailer
인스턴스를 만들어야합니다. 대부분의 코드는 Laravel의 register
메서드에서 복사 할 수 있습니다.config/app.php
파일에 Illuminate\Mail\MailServiceProvider::class,
을 사용자의 공급자로 바꿉니다.이렇게하면 Laravel 's Mail 기능을 사용할 수 있습니다.
자세한 내용은 유사한 문제를 해결하는 다음 질문/답변을 참조하십시오. Mail 기능을 확장하여 새 전송 드라이버를 추가하지만 자체 Mailer 구현 및 서비스 제공 업체를 제공한다는 점에서 유사한 접근 방식을 취합니다.
Add a new transport driver to Laravel's Mailer
응용 프로그램/MyMailer/Mailer.php
<?php
namespace App\MyMailer;
class Mailer extends \Illuminate\Mail\Mailer
{
public function send($view, array $data = [], $callback = null)
{
// Do your checks
return parent::send($view, $data, $callback);
}
}
응용 프로그램/MyMailer/MailServiceProvider.php
(Laravel의 MailServiceProvider 클래스에서 복사 한 코드의 대부분)<?php
namespace App\MyMailer;
class MailServiceProvider extends \Illuminate\Mail\MailServiceProvider
{
public function register()
{
$this->registerSwiftMailer();
$this->app->singleton('mailer', function ($app) {
// This is YOUR mailer - notice there are no `use`s at the top which
// Looks for a Mailer class in this namespace
$mailer = new Mailer(
$app['view'], $app['swift.mailer'], $app['events']
);
$this->setMailerDependencies($mailer, $app);
$from = $app['config']['mail.from'];
if (is_array($from) && isset($from['address'])) {
$mailer->alwaysFrom($from['address'], $from['name']);
}
$to = $app['config']['mail.to'];
if (is_array($to) && isset($to['address'])) {
$mailer->alwaysTo($to['address'], $to['name']);
}
return $mailer;
});
}
}
설정 (공급자 배열)/app.php
//...
// Illuminate\Mail\MailServiceProvider::class,
App\MyMailer\MailServiceProvider::class,
//...
당신은 메일러가 검사를 수행하지 말았어야, 즉 그것의 일이 아니다, 그냥 메일을 보냅니다. 로직은 외부에서 수행되어야하며, 메일이 전송되어야한다면 send 메소드를 호출하십시오. – tam5
@tam 당신은 유지 보수와 구조에 대해 이야기하고 있습니다. 나는 알고 있습니다. 우편물 책임자에게 책임을 전가하지 않는 것이 좋습니다. 그러나 문제는 그것에 관한 것이 아닙니다. – naneri