我正在使用Laravel 5.2创建一个Web应用程序。我已成功登录到应用程序,但当我尝试注销时。它不允许我这样做。
当我调查时,我发现\Illuminate\Support\Facades\Auth::logout()
使用dd(\Illuminate\Support\Facades\Auth::logout());
返回null。我也试过$this-auth->logout();
这个语句也返回null。
我没有使用默认的laravel脚手架,而是创建了我的usercontroller并做了同样的事情。
努力:
Route.php
Route::get('/', 'HomeController@index');
Route::get('logout/','UserController@logout');
Route::group(['middleware' => ['web']], function () {
Route::get('login/','UserController@loginForm');
Route::post('login/','UserController@login');
Route::get('register/','UserController@register');
Route::post('register/','UserController@store');
Route::get('home/',['as' => 'home', 'uses' => 'HomeController@index']);
});
UserController.php
namespace App\Http\Controllers;
use App\User;
use App\Profile;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* User model instance
* @var User
*/
protected $user;
protected $profile;
/**
* For Guard
*
* @var Authenticator
*/
protected $auth;
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct(Guard $auth, User $user)
{
$this->user = $user;
$this->auth = $auth;
$this->middleware('guest', ['except' => 'logout']);
}
public function login(Request $request)
{
if ($this->auth->attempt($request->only('email', 'password'))) {
// dd(\Illuminate\Support\Facades\Auth::user());
dd(\Illuminate\Support\Facades\Auth::user());
return redirect()->route('home');
}
return redirect('login')->withErrors([
'email' => 'The email or the password is invalid. Please try again.',
]);
}
/**
* Log the user out of the application.
*
* @return Response
*/
protected function logout()
{
// \Illuminate\Support\Facades\Auth::logout();
dd(\Illuminate\Support\Facades\Auth::logout());
$this->auth->logout();
\Session::flush();
return redirect('login');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function loginForm()
{
return view('user.login', ['title' => 'Login Page']);
}
....
}
我很清楚为什么用户没有注销?请帮帮我。