我是laravel
的新手。我创建了项目并创建了
php artisan make:migration create_vendors_table
然后我创建了
这样的模型php artisan make:model Vendor
我修改了表并进行了迁移。在MySQL中成功创建了表。但是,当我尝试打http://localhost:8000/api/v1/register时,我遇到了以下问题:
Symfony\Component\Debug\Exception\FatalErrorException: Class 'Illuminate\Foundation\Auth\Vendor' not found in file /Users/sandeeparmal/clients/ForCity/code/webservices/php_api/forcity_web_api/app/Vendor.php on line 8
我试图在Illuminate\Foundation\Auth\
中找到Vendors文件,但该文件不可用。
这是我的Vendor.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\Vendor as Authenticatable;
use Laravel\Passport\HasApiTokens;
class Vendor extends Authenticatable
{
use Notifiable;
use HasApiTokens;
protected $guard = 'vendors';
protected $hidden = [
'password', 'api_key',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
这是我的auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'vendors',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'vendors',
],
'api' => [
'driver' => 'passport',
'provider' => 'vendors',
],
],
'providers' => [
'vendors' => [
'driver' => 'eloquent',
'model' => App\Vendor::class,
],
],
这是我的AuthController.php
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Vendor;
use Illuminate\Support\Facades\Auth;
use Validator;
class AuthController extends Controller
{
public $successStatus = 200;
public function register(Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required',
'number' => 'required',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$vendor = Vendor::create($input);
$success['token'] = $vendor->createToken('AppName')->accessToken;
return response()->json(['success'=>$success], $this->successStatus);
}
public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$vendor = Auth::vendor();
$success['token'] = $vendor->createToken('AppName')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
} else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
public function getVendor() {
$vendor = Auth::vendor();
return response()->json(['success' => $vendor], $this->successStatus);
}
}
我还尝试了作曲家的转储自动加载
答案 0 :(得分:1)
您的use
语句应为:
use Illuminate\Foundation\Auth\User as Authenticatable;
不是这个
use Illuminate\Foundation\Auth\Vendor as Authenticatable;
答案 1 :(得分:0)
您只是选择不使用User
模型进行身份验证,因为要使用laravel的内置身份验证功能,但要使用自定义模型,因此Vendor
模型必须扩展Illuminate\Foundation\Auth\User
将提供给Vendor
模型。 Laravel在身份验证期间使用的所有身份验证功能和方法,而不是不存在的Illuminate\Foundation\Auth\Vendor
(Illumiate\Foundation\Auth
是laravel的namspace`,而不是您的)
即使您创建自定义Vendor
类,它也不能位于laravel使用的名称空间Illuminate \ Foundation \ Auth`名称空间中,您只能访问创建的名称空间,而不能访问第三个名称空间政党图书馆。