【问题标题】:Laravel 4 re-factoring contact form from controller to modelLaravel 4 重构从控制器到模型的联系表格
【发布时间】:2014-03-09 23:30:58
【问题描述】:

在我开始重构这段代码之前,它确实有效。在将所有业务逻辑移至模型并向控制器添加 try/catch 块后,一切似乎都正确。当我填写表格并点击发送时,我收到一条成功消息,但没有收到电子邮件。我哪里错了?

控制器

class ContactController extends BaseController {

    protected $contact;

    public function __construct(Contact $contact)
    {
        $this->beforeFilter('csrf', array('on' => 'post'));
        $this->contact = $contact;
    }

    public function serve()
    {
        return View::make('layouts.contact');
    }

    public function store()
    {
        try
        {
            $this->contact->sendMessage(Input::all());
        }

       catch (ValidationError $e)
       {
           return Redirect::back()
                ->withInput()
                ->withErrors($e->getErrors());
       }

    return Redirect::to('contact')
            ->with('message', 'Your message was successfully sent!');
    }
}

型号

class Contact extends Eloquent {

    public function sendMessage($input)
    {
        $validation = new Services\Validators\Contact;

        if($validation->passes())
        {
            $fromEmail = Input::get('email');
            $fromName = Input::get('name');
            $subject = "Email from user";
            $data = [ 'msg' => Input::get('message') ];

            $toEmail = 'logistics@praestavi.com';
            $toName = 'Mitch Glenn';

            Mail::send('emails.contact', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){

                $message->to($toEmail, $toName);

                $message->from($fromEmail, $fromName);

                $message->subject($subject);
            });
        }

        else
        {
            $this->errors = $validation->errors;
            throw new ValidationError($validation->errors);
        }
    }
}

重新总结问题:表单有效,但提交并收到成功消息后,我没有收到电子邮件。感谢您花时间查看我的代码。

编辑:添加请求的验证器

Contact.php

<?php namespace Services\Validators;

class Contact extends Validator {

    public static $rules = [
        'name' => 'required',
        'email' => 'required|email',
        'message' => 'required'
    ];
}

Validator.php

<?php namespace Services\Validators;

abstract class Validator {

    protected $attributes;
    public $errors;

    public function __construct($attributes = null)
    {
    $this->attributes = $attributes ?: \Input::all();
    }

    public function passes()
    {
        $validation = \Validator::make($this->attributes, static::$rules);

        if ($validation->passes()) return true;

        $this->errors = $validation->messages();

        return false;
    }
}    

【问题讨论】:

  • 就像他们说的:生活和学习!
  • 嗯,你的“邮件”模块可能有问题?
  • @neoascetic 我不确定它会有什么问题,因为它在我将它从控制器移入模型之前就已经工作了。
  • 请出示您的联系人验证器

标签: php laravel laravel-4 eloquent swiftmailer


【解决方案1】:

假设您只是在这里重构了代码,我将开始围绕Mail::send() 调用添加一些调试。也许将它包装在 dd() 函数中。你应该看到一个整数;邮件成功发送到的收件人数量。

另外,您的app/config/mail.php 内容是什么样的(请删除所有敏感密码)。

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 2013-07-05
    • 2013-09-24
    • 1970-01-01
    • 2014-10-19
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多