Laravel:了解“凭证”方法机制

时间:2017-11-26 12:11:18

标签: php laravel

我需要自定义身份验证,即:

  • LoginController的自定义模型和数据库表(已解决)
  • “订单ID”而不是用于身份验证的电子邮件(已解决)

因为我不理解credentials方法机制,所以我收到错误:

Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() 
must be an instance of Illuminate\Contracts\Auth\Authenticatable,  
instance of App\Order given, called in C:\OpenServer\domains\sites-in-development.loc\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line

enter image description here

<?php

class LoginController extends Controller {

    use AuthenticatesUsers;

    protected $redirectTo; 
    protected $orderAlias;

    protected $loginView;

    /*
        Here I set the order ID as "username". I don't like this method,
        because it causes the confusion ($username is actually order ID),
        but don't know the other way yet.
        REF is just the class with array contains the string values to avoid hardcoding
    */
    protected $username = REF::tables['orders']['fieldNames']['orderId'];


    public function __construct() {

        $this->middleware('guest')->except('logout');
        $this->loginView = '00_top';

    }


    public function showLoginForm() {
        // Work properly
    }


    public function login(Request $request) {

        if ($this->hasTooManyLoginAttempts($request)) {

            $this->fireLockoutEvent($request);
            return $this->sendLockoutResponse($request);
        }


        // manual validation (work properly)
        // getting $orderAlias from DB

        $redirectTo = '/'.$orderAlias;

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request); // to "credentials"... 
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);

    }

    protected function credentials(Request $request) {

        // here is error
        return $request->only(REF::forms['loginToOrder']['fieldNames']['orderID'], 
                REF::forms['loginToOrder']['fieldNames']['password']);
    }
}

0 个答案:

没有答案