2017-09-11 4 views

答えて

0

メーラークラスを作成し、アクションに挿入してメールを送信する必要があります。おそらくあなたはいくつかのアクションでメーラークラスを必要とするでしょう。そのため、意識的な特性がいいので、__constructメソッドのあらゆるアクションにそれを注入する必要はありません。私はそれが問題を解決できるようなものだと思うので、あなたはどこでもあなたのメーラーサービスを使うことができます。ちょうどそれを注入することを忘れないでください。

interface MailServiceInterface 
{ 
    public function send(string $to, string $from, string $subject, string $body, array $headers = []); 
} 

trait MailServiceAwareTrait 
{ 
    /** 
    * @var \Infrastructure\Mailer\MailServiceInterface 
    */ 
    protected $mailService; 

    public function setMailService(MailServiceInterface $mailService) 
    { 
     $this->mailService = $mailService; 
    } 

    public function getMailService(): MailServiceInterface 
    { 
     return $this->mailService; 
    } 
} 

class myAction extends AbstractActionControl 
{ 
    use MailServiceAwareTrait; 

    public function processAction() 
    { 
     $this->getMailService()->send($to, $from, $subject, $body); 
    } 
} 
0

ので、典型的には、別個のモデルファイル(別名サービス・ファイル)ではなく、コントローラでなければならないサービスである「電子メールを送信」。実際にはコントローラーに関数として入れることができますが、それは単にMVCの概念自体を完全に誤用していることを意味します。

とにかく、私はそれを行う方法に答えるでしょうしかし、私は強くそれをお勧めしません。あなたのコントローラー(例えば、IndexController)で、これはあなたができることです:

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

class IndexController extends AbstractActionController { 
    public function indexAction() { 
     // This below line will call FooController's barAction() 
     $otherViewModel = $this->forward()->dispatch(\Application\Controller\FooController::class, ['action'=>'bar']); 
     $otherViewModel->setTemplate('application/foo/bar');// you must set which template does this view use 
     return $otherViewModel; 
    } 
} 
関連する問題