简体   繁体   English

Laravel 5.1:Auth::logout() 不起作用并且目标 [Illuminate\\Contracts\\Auth\\Registrar] 不是可实例化的错误

[英]Laravel 5.1: Auth::logout() is not working and Target [Illuminate\Contracts\Auth\Registrar] is not instantiable error

I'm new in laravel and I try to build a simple Authentication.我是 laravel 的新手,我尝试构建一个简单的身份验证。 all the things works fine except when using AuthController's action to log out, it just simply doesn't work.除了使用 AuthController 的操作注销之外,所有事情都正常工作,只是根本不起作用。 I have a nav bar which checks for Auth::check() and it doesn't change after calling the log out action and it stocks in home page.我有一个导航栏,用于检查 Auth::check() 并且在调用注销操作后它不会改变并且它在主页中存有。 but after making few changes like deleting this manual logout code:但是在进行了一些更改(例如删除此手动注销代码)之后:

public function logout()
    {
        Auth::logout();
        Session::flush();
        return redirect()->intended('login');
    }

and changing the rout from this:并从此改变路线:

Route::get('logout', 'SessionsController@logout');

to this:对此:

Route::get('auth/logout', 'Auth\AuthController@logout');

it throws this error:它抛出这个错误:

BindingResolutionException in Container.php line 749: Target [Illuminate\Contracts\Auth\Registrar] is not instantiable. 

I have this route inside the routes.php file:我在 routes.php 文件中有这条路线:

Route::get('auth/logout', 'Auth\AuthController@logout');

and my AuthController is the controller that laravel providing us.而我的 AuthController 是 Laravel 为我们提供的控制器。

my SessionsController that handled logout before the changes but actually not work either is:我在更改之前处理注销但实际上不起作用的 SessionsController 是:

<?php namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class SessionsController extends Controller
{
    /**
     * Create a new sessions controller instance.
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the login page.
     *
     * @return \Response
     */
    public function login()
    {
        return view('auth.login');
    }

    /**
     * Perform the login.
     *
     * @param  Request  $request
     * @return \Redirect
     */
    public function postLogin(Request $request)
    {
        $this->validate($request, ['username' => 'required|exists:users', 'password' => 'required']);

        if ($this->signIn($request)) {
            flash('Welcome back!');

            return redirect()->intended('dashboard');

        }

        flash('Could not sign you in.');

        return redirect()->back();
    }

    /**
     * Destroy the user's current session.
     *
     * @return \Redirect
     */
    public function logout()
    {
        Auth::logout();
        Session::flush();
        return redirect()->intended('login');
    }

    /**
     * Attempt to sign in the user.
     *
     * @param  Request $request
     * @return boolean
     */
    protected function signIn(Request $request)
    {
        return Auth::attempt($this->getCredentials($request), $request->has('remember'));
    }

    /**
     * Get the login credentials and requirements.
     *
     * @param  Request $request
     * @return array
     */
    protected function getCredentials(Request $request)
    {
        return [
            'username' => $request->input('username'),
            'password' => $request->input('password'),
            'verified' => true
        ];
    }


}

And here is the AuthController __construct method:这是 AuthController __construct 方法:

public function __construct(Guard $auth, Registrar $registrar)
    {
        $this->auth = $auth;
        $this->registrar = $registrar;
        $this->middleware('guest', ['except' => ['logout', 'getLogout']]);

    }

Please help me as soon as possible.请尽快帮助我。 Any help would be appreciated.任何帮助,将不胜感激。

update your route by this one, this is the default logout method present in AuthController Trait通过这个更新你的路由,这是AuthController Trait 中的默认注销方法

Route::get('auth/logout', 'Auth\AuthController@getLogout');

Add this line in your AuthController, this will redirect your application after Logout has done.在您的 AuthController 中添加这一行,这将在注销完成后重定向您的应用程序。

Note : you can change the path as per your requirement.注意:您可以根据需要更改路径。

class AuthController extends Controller
{
   protected $redirectAfterLogout  =   '/auth/login'; // this line

try this尝试这个

public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

I finally found the problem and fixed it with the solution below:我终于找到了问题并使用以下解决方案修复了它:

I change the rout to:我将路线更改为:

Route::get('auth/logout', 'Auth\AuthController@getLogout');

and change the __construct method in AuthController to:并将 AuthController 中的 __construct 方法更改为:

public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
    }

1.The problem was in the rout. 1.问题出在溃败上。 Instead of calling method "getlogout" I call for the "logout" method in my routs.php .我没有调用方法“getlogout”,而是在 routs.php 中调用“logout”方法。
2. In 'except' => ['logout', 'getLogout'] , what ever method you call in your routs.php you must add to this except in AuthController __construct method. 2. 在 'except' => ['logout', 'getLogout'] 中,您在 routs.php 中调用的任何方法都必须添加到其中,除了 AuthController __construct 方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel “目标 [Illuminate\\Contracts\\Bus\\Dispatcher] 不可实例化。” - Laravel "Target [Illuminate\Contracts\Bus\Dispatcher] is not instantiable." Laravel 5.8 目标 [Illuminate\Contracts\Debug\ExceptionHandler] 不可实例化 - Laravel 5.8 Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable Laravel致命错误:致命错误:找不到接口&#39;Illuminate \\ Auth \\ Illuminate \\ Contracts \\ Auth \\ Factory&#39; - Laravel fatal error: Fatal error: Interface 'Illuminate\Auth\Illuminate\Contracts\Auth\Factory' not found PHPUnit 给出错误:目标 [Illuminate\Contracts\View\Factory] 不可实例化 - PHPUnit gives error: Target [Illuminate\Contracts\View\Factory] is not instantiable 目标 [Illuminate\\Contracts\\Debug\\ExceptionHandler] 不可实例化 - Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable 目标 [Illuminate\Contracts\Broadcasting\Factory] 不可实例化 - Target [Illuminate\Contracts\Broadcasting\Factory] is not instantiable Laravel 5 身份验证注销不起作用 - Laravel 5 Auth logout not working Laravel错误:无法实例化目标[Illuminate \\ View \\ Engines \\ EngineInterface] - Laravel error: Target [Illuminate\View\Engines\EngineInterface] is not instantiable Laravel 8:传递给 Illuminate\Auth\SessionGuard::__construct() 的参数 2 必须是 Illuminate\Contracts\Auth\UserProvider 的实例,null 给定 - Laravel 8 : Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given 目标 [Laravel\Fortify\Contracts\RegisterViewResponse] 不可实例化 - target [Laravel\Fortify\Contracts\RegisterViewResponse] is not instantiable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM