【问题标题】:Laravel giving invalid view error for mailLaravel 给出邮件的无效视图错误
【发布时间】:2019-12-24 10:48:03
【问题描述】:

我是 Laravel 的新手。我想在用户每次登录时向他们发送邮件。我已经写好了认证逻辑。

<?php

namespace App\Http\Controllers\Users;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Events\Login;

class LoginController extends Controller
{
    public function __construct(){
        $this->middleware('guest')->except('logout');
    }

    public function index(){
        return view('users.login');
    }

    public function login(Request $request){
        $credentials = $request->only('username', 'password');
        if(Auth::attempt($credentials)){
            event(new Login(auth()->user()));
            return redirect()->intended(route('homepage'));
        }else{
            return redirect()->back()->withInput()->with('error', 'Username/Password Combo Wrong!');
        }

    }

    public function logout(){
        Auth::logout();
        return redirect()->route('login-form');
    }
}

我为此写了一个活动

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;

class Login
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $user;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }



    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

这是事件监听器

<?php

namespace App\Listeners;

use App\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Request;
use App\Mail\UserLoggedIn;

class SendLoginNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  Login  $event
     * @return void
     */
    public function handle(Login $event)
    {
        Mail::send($event)->send(new UserLoggedIn($event));
    }
}

我创建了一个邮件类

<?php

namespace App\Mail;

use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class UserLoggedIn extends Mailable
{
    use Queueable, SerializesModels;
    public $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('test@mail.com')->view('emails.users.loggedin');
    }
}

然后我在 resources/views/emails/users/loggedin.blade.php 中为邮件创建了视图

<!Doctype html>
<html>
<head>
    <title>User Logged In</title>
</head>
<body>
    {{$user->name}} Logged in
</body>
</html>

但是当我运行尝试运行登录时,我收到一个错误提示无效视图

【问题讨论】:

  • 仅供参考,Laravel 已经有一个登录帐户 event (Illuminate\Auth\Events\Login)。同样,使用通知可能比使用邮件更容易。
  • 你的首页路由在哪里??

标签: laravel email events


【解决方案1】:

我注意到了几个问题。目前,您是:

  • 在您的SendLoginNotification 中调用send 两次(并且您将Event 传递给其中一个而不是可邮寄的)
  • 未指定将邮件发送给谁
  • 还将$event 传递给可邮寄的而不是User

变化:

Mail::send($event)->send(new UserLoggedIn($event));

收件人:

Mail::to($event->user)->send(new UserLoggedIn($event->user)); //notice the "user" property.

【讨论】:

    【解决方案2】:

    您没有传递任何包含用户信息的变量。

    public function build()
    {
        $user=User::find(1);
        return $this->from('test@mail.com')->view('emails.users.loggedin',compact('user'));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2014-07-21
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多