我无法理解如何使用Laravel 5.3的新Mailable功能将Request数据传递给邮件 使用下面的代码我得到了没有定义变量$ request的错误。
web.php
Route::get('/', ['as' => 'home', 'uses' => 'HomeController@index']);
Route::get('/callback', ['as' => 'callback.show', 'uses' => 'PostController@callbackshow']);
Route::post('/callback', ['as' => 'callback.send', 'uses' => 'PostController@callbacksend']);
控制器\ PostController.php
<?php namespace App\Http\Controllers;
use App\Mail\Callback;
use Illuminate\Http\Request;
class PostController extends Controller {
public function __construct()
{
}
public function callbackshow()
{
return view('callback');
}
public function callbacksend(Request $request)
{
$email = new Callback($request);
\Mail::to('test@gmail.com')->send($email);
}
邮件\ Callback.php
namespace App\Mail;
use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Callback extends Mailable
{
use Queueable, SerializesModels;
public function __construct(Request $request)
{
$this->request = $request->all();
}
public function build()
{
return $this->view('emails.callback');
}
}
电子邮件\ callback.blade.php
Name: {{ $request->name }}<br>
Email : {{ $request->email }}<br>
这是错误
Whoops, looks like something went wrong.
2/2 ErrorException in 76aac56b59bc685390113940ddc975544836b896.php line 2: Undefined variable: request (View: D:\OpenServer\domains\blog2\resources\views\emails\callback.blade.php)
in 76aac56b59bc685390113940ddc975544836b896.php line 2
at CompilerEngine->handleViewException(object(ErrorException), '0') in PhpEngine.php line 44
at PhpEngine->evaluatePath('D:\OpenServer\domains\blog2\storage\framework\views/76aac56b59bc685390113940ddc975544836b896.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in CompilerEngine.php line 59
at CompilerEngine->get('D:\OpenServer\domains\blog2\resources\views/emails/callback.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Mailer.php line 430
at Mailer->getView('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 310
at Mailer->addContent(object(Message), 'emails.callback', null, null, array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 186
at Mailer->send('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null), object(Closure)) in Mailable.php line 117
at Mailable->send(object(Mailer)) in Mailer.php line 173
at Mailer->send(object(Callback)) in MailableMailer.php line 99
at MailableMailer->send(object(Callback)) in PostController.php line 24
at PostController->callbacksend(object(Request))
at call_user_func_array(array(object(PostController), 'callbacksend'), array(object(Request))) in Controller.php line 55
at Controller->callAction('callbacksend', array(object(Request))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(PostController), 'callbacksend') in Route.php line 190
at Route->runController() in Route.php line 144
at Route->run(object(Request)) in Router.php line 642
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 644
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 54
1/2 ErrorException in 76aac56b59bc685390113940ddc975544836b896.php line 2: Undefined variable: request
in 76aac56b59bc685390113940ddc975544836b896.php line 2
at HandleExceptions->handleError('8', 'Undefined variable: request', 'D:\OpenServer\domains\blog2\storage\framework\views\76aac56b59bc685390113940ddc975544836b896.php', '2', array('__path' => 'D:\OpenServer\domains\blog2\storage\framework\views/76aac56b59bc685390113940ddc975544836b896.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message)), 'obLevel' => '0', '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in 76aac56b59bc685390113940ddc975544836b896.php line 2
at include('D:\OpenServer\domains\blog2\storage\framework\views\76aac56b59bc685390113940ddc975544836b896.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('D:\OpenServer\domains\blog2\storage\framework\views/76aac56b59bc685390113940ddc975544836b896.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in CompilerEngine.php line 59
at CompilerEngine->get('D:\OpenServer\domains\blog2\resources\views/emails/callback.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in View.php line 149
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Mailer.php line 430
at Mailer->getView('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 310
at Mailer->addContent(object(Message), 'emails.callback', null, null, array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 186
at Mailer->send('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null), object(Closure)) in Mailable.php line 117
at Mailable->send(object(Mailer)) in Mailer.php line 173
at Mailer->send(object(Callback)) in MailableMailer.php line 99
at MailableMailer->send(object(Callback)) in PostController.php line 24
at PostController->callbacksend(object(Request))
at call_user_func_array(array(object(PostController), 'callbacksend'), array(object(Request))) in Controller.php line 55
at Controller->callAction('callbacksend', array(object(Request))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(PostController), 'callbacksend') in Route.php line 190
at Route->runController() in Route.php line 144
at Route->run(object(Request)) in Router.php line 642
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Router.php line 644
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 54
答案 0 :(得分:1)
我成功解决了这个问题
namespace App\Mail;
use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Callback extends Mailable
{
use Queueable, SerializesModels;
public $input;
public function __construct(Request $request)
{
$this->input = $request->all();
if (empty($this->input['email'])) { $this->input['email'] = 'admin@site.ru'; }
}
public function build()
{
return $this->subject('Обратный звонок с сайта')
->view('emails.callback')
->cc($this->input['email'], $this->input['name'])
->replyTo($this->input['email'], $this->input['name']);
}
}