我已经使用护照实现了laravel api身份验证,它在我的本地环境中有效。但是,当我将应用程序部署到cPanel服务器时,它不起作用。并尝试登录或注册时返回此错误
例外:“ Symfony \ Component \ Debug \ Exception \ FatalErrorException” 文件: “ /home/newalgeria/new_algeria/vendor/zendframework/zend-diactoros/src/PhpInputStream.php” 行:17消息:“找不到类'Zend \ Diactoros \ Stream'”跟踪:[]
我按照以下步骤进行了设置:
1-
composer require laravel/passport
2-我将此行添加到config / app.php中的提供程序
'providers' =>[
Laravel\Passport\PassportServiceProvider::class,
],
3-
php artisan passport:install
php artisan migrate
4-将hasApiToken添加到用户模型
use Laravel\Passport\HasApiTokens;
...
...
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
...
...
..
}
5-将此添加到app / Providers / AuthServiceProvider.php
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
6-在config / auth.php中将驱动程序更改为通行证
...
..
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
...
..
6-创建了我的路线
Route::post('login', 'API\UserController@login');
Route::post('register', 'API\UserController@register');
Route::group(['middleware' => 'auth:api'], function(){
Route::post('details', 'API\UserController@details');
});
7-然后是我的身份验证控制器(##在createtoken()函数##上发现问题的位置)
/**
* login api
*
* @return \Illuminate\Http\Response
*/
public function login(){
if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
/**
* Register api
*
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$input = $request->all();
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|unique:users',
'password' => 'required',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('MyApp')-> accessToken;
$success['name'] = $user->name;
return response()->json(['success'=>$success], $this-> successStatus);
}
/**
* details api
*
* @return \Illuminate\Http\Response
*/
public function details()
{
$user = Auth::user();
return response()->json(['success' => $user], $this-> successStatus);
}
1-我试图在服务器上运行
run compsoer php passport:install
再次创建新的客户ID,但没有出现相同的错误
2-我试图用相同的东西更新作曲家
3-我试图注释此行( where createToke('MyApp'))在我的注册函数上的身份验证控制器上
$success['token'] = $user->createToken('MyApp')-> accessToken;
它创建了新用户,但没有令牌==>这意味着问题出在此函数中吗?但适用于本地
我一次又一次收到此错误
例外:“ Symfony \ Component \ Debug \ Exception \ FatalErrorException” 文件: “ /home/newalgeria/new_algeria/vendor/zendframework/zend-diactoros/src/PhpInputStream.php” 行:17消息:“找不到类'Zend \ Diactoros \ Stream'”跟踪:[]
我使用的是larvel 5.8,需要php> = 7.1,但是。但是在我的服务器终端中,当我运行php -v时,它表明php的版本是5.6,但是在浏览器中当我测试phpversion()时;它返回php 7.2