【问题标题】:Adding Event to AuthController in Laravel 5.2在 Laravel 5.2 中向 AuthController 添加事件
【发布时间】:2017-04-23 00:57:03
【问题描述】:

在我只是想帮助澄清它之前,我看到了这个问题。

目标是在有人注册到网站时触发事件。

我在事件服务提供者中添加了事件/侦听器对,如 Laravel 文档中所述。

然后我运行了:php artisan 命令 php工匠生成:事件

当用户在 example.com/register 上注册时,它应该会触发该事件。然而事实并非如此。

有什么建议吗?

app/Http/Controllers/Auth/AuthController.php

 protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
        Event::fire(new NewUserSignUp($data));
    }

整个文件:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use App\Events\NotifyAdminSignUp;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/';

/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
    Event::fire(new NewUserSignUp($data));
}



}

【问题讨论】:

    标签: php laravel


    【解决方案1】:
    protected function create(array $data)
    { 
        //fire event BEFORE return
        Event::fire(new NewUserSignUp($data));
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
        //because nothing after this line will ever be processed
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 2016-11-07
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多