使 Laravel 5 注册成为现有用户的一项任务
Make Laravel 5 registration a task for existing users
我想重新配置默认的 Laravel 注册方法,以便只有现有用户才能注册新用户。但是,我不确定如何重新配置我的 AuthController,以便 getRegister 函数不会在用户已经登录时自动重定向到 /Home。
理想情况下,用户应该能够登录,点击 "Register New User" link,然后使用通常的注册用户表单。完成后,它应该插入新的用户凭据,然后将他们重定向到他们的主页,而不改变他们的用户会话状态。
似乎只需要在 Auth 控制器中增加一两行即可禁用注册方法的重定向,但我似乎无法从 Laravel 文档中弄清楚。谁能帮帮我?
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
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;
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
public function getRegister() {
$this->middleware('auth');
parent::getRegister();
}
public function postRegister() {
return redirect('/');
}
}
您需要做的就是在您的 AuthController
.
中添加一个 属性
protected $redirectPath = '/wherever/you/need/to/go';
那应该处理一下。您还可以设置其他属性来处理内部身份验证系统,这些属性在文档中有所描述。
http://laravel.com/docs/5.1/authentication
我认为您可以更改以下内容以排除注册方法..
$this->middleware('guest', ['except' => 'getLogout']);
唯一的问题是当您的用户注册时,他们将自动以该用户身份登录。我能想到的一种绕过可能被认为有点 hacky 的方法是将当前登录的用户保存在 AuthController
的构造函数中
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => ['getLogout', 'getRegister', 'postRegister']]);
$this->user = Auth::check() ? Auth::user() : null;
}
然后你可以添加一个析构函数来检查那个变量,如果它在那里,让你的用户以他自己的身份重新登录。
/**
* Logs the user back in as himself in the event he has registered a new user.
*
* @return void
*/
public function __destruct()
{
if(!is_null($this->user)) {
Auth::login($this->user);
}
}
我想重新配置默认的 Laravel 注册方法,以便只有现有用户才能注册新用户。但是,我不确定如何重新配置我的 AuthController,以便 getRegister 函数不会在用户已经登录时自动重定向到 /Home。
理想情况下,用户应该能够登录,点击 "Register New User" link,然后使用通常的注册用户表单。完成后,它应该插入新的用户凭据,然后将他们重定向到他们的主页,而不改变他们的用户会话状态。
似乎只需要在 Auth 控制器中增加一两行即可禁用注册方法的重定向,但我似乎无法从 Laravel 文档中弄清楚。谁能帮帮我?
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
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;
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
public function getRegister() {
$this->middleware('auth');
parent::getRegister();
}
public function postRegister() {
return redirect('/');
}
}
您需要做的就是在您的 AuthController
.
protected $redirectPath = '/wherever/you/need/to/go';
那应该处理一下。您还可以设置其他属性来处理内部身份验证系统,这些属性在文档中有所描述。
http://laravel.com/docs/5.1/authentication
我认为您可以更改以下内容以排除注册方法..
$this->middleware('guest', ['except' => 'getLogout']);
唯一的问题是当您的用户注册时,他们将自动以该用户身份登录。我能想到的一种绕过可能被认为有点 hacky 的方法是将当前登录的用户保存在 AuthController
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => ['getLogout', 'getRegister', 'postRegister']]);
$this->user = Auth::check() ? Auth::user() : null;
}
然后你可以添加一个析构函数来检查那个变量,如果它在那里,让你的用户以他自己的身份重新登录。
/**
* Logs the user back in as himself in the event he has registered a new user.
*
* @return void
*/
public function __destruct()
{
if(!is_null($this->user)) {
Auth::login($this->user);
}
}