이 코드를 다시 고려하기 전에 작동했습니다. 모든 비즈니스 로직을 모델로 옮기고 컨트롤러에 try/catch 블록을 추가 한 후에 모든 것이 올바른 것처럼 보입니다. 양식을 작성하고 보내기를 누르면 성공 메시지가 표시되지만 이메일을받지 못합니다. 내가 어디로 잘못 가고 있니?Laravel 4 컨트롤러에서 모델로 문의 양식 다시 작성
컨트롤러
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 = '[email protected]';
$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;
}
}
그들이 말하는 것 : 살아 있고 배우십시오! –
흠, "Mail"모듈에 문제가있을 수 있습니까? – neoascetic
@neoascetic 컨트롤러에서 모델로 이동하기 전에 작동했기 때문에 무엇이 잘못 될지 잘 모르겠습니다. –