找不到用于邮件的Laravel类

时间:2019-03-18 06:43:35

标签: laravel class email namespaces laravel-mail

当我正在构建的网站上有新注册时,我正在尝试向网站管理员生成通知电子邮件。

我在App \ Mail \ SignedUp.php中有一个名为SignedUp的邮件类。

在Illuminate / Foundation / Auth / RegistersUser文件中,我具有以下内容:

namespace Illuminate\Foundation\Auth;

use App\Organization;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;
use App\Mail\SignedUp;

trait RegistersUsers
{
    use RedirectsUsers;

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        $organizations = Organization::all();

        return view('auth.register', compact('organizations'));
    }

    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {

        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        Mail::to('example@example.com')->send(new SignedUp($user));        

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

注册新用户时,出现以下消息: 找不到“ Illuminate \ Foundation \ Auth \ Mail”类

如果我将use App\Mail\SignedUp;更改为use App\Mail;,则错误仍然:找不到类'Illuminate \ Foundation \ Auth \ Mail'

use Mail;给了我找不到Illuminate \ Foundation \ Auth \ SignedUp'

use App\Mail\SignedUp;给了我找不到类'Illuminate \ Foundation \ Auth \ Mail'

根本不尝试导入该类,而是将我的声明更改为:\Mail::to('example@example.com')->send(new SignedUp($user));,我得到找不到类'Illuminate \ Foundation \ Auth \ SignedUp'

A无法弄清楚如何找到我的邮件类别。

我只是在黑暗中猜测和射击-有人能看到我在做什么吗?

这是我的邮件类别文件:

<?php

namespace App\Mail;

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

class SignedUp extends Mailable
{
    use Queueable, SerializesModels;

    public $url = 'https://av-cuauhtemoc.org/users';

    public $user;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('example@example.com')->view('emails.new-registration');
    }
}

2 个答案:

答案 0 :(得分:2)

我终于迷失了答案。 我还需要导入use Illuminate\Support\Facades\Mail;类。

这可行:

use App\Mail\Signedup;
use Illuminate\Support\Facades\Mail;

答案 1 :(得分:0)

首先,您不应该更改Illuminate/Foundation/Auth/RegistersUser文件,而必须在RegisterController中覆盖此方法:

use App\Mail\SignedUp;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Mail;

public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        Mail::to('example@example.com')->send(new SignedUp($user));

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }