Laravel Fortify,Vuex。数组到字符串的转换错误。 FortifyServiceProvider.php 第 40 行。发生了啥事?
Posted
技术标签:
【中文标题】Laravel Fortify,Vuex。数组到字符串的转换错误。 FortifyServiceProvider.php 第 40 行。发生了啥事?【英文标题】:Laravel Fortify, Vuex. Array to string conversion error. FortifyServiceProvider.php line 40. What's going on?Laravel Fortify,Vuex。数组到字符串的转换错误。 FortifyServiceProvider.php 第 40 行。发生了什么事? 【发布时间】:2022-01-05 05:06:57 【问题描述】:我花了几乎一整天的时间来弄清楚发生了什么。我有登录 vue 组件,需要将电子邮件和密码值发送到服务器。但是登录控制器是内置在 Fortify 控制器中的,所以我不知道为什么会出现错误 500 和错误 422(我得到它们的时间不同,而不是同一时间,但它们是我用各种不同的方法得到的唯一结果实验)。我为 vuex 存储方法和模块创建了 js 文件,并在模块中使用了 axios.post,在登录 vue 组件中使用了 this.$store.dispatch。
这是代码的重要部分
登录.vue
<template>
<form>
<div class="input-group form-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<label>
<input type="email" v-model="email" name="email" class="form-control" placeholder="email">
</label>
</div>
<div class="input-group form-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-key"></i></span>
</div>
<label>
<input type="password" v-model = "password" name="password" class="form-control" placeholder="password">
</label>
</div>
<div class="row align-items-center remember">
<label>
<input type="checkbox">
</label>Remember Me
</div>
<div class="form-group">
<input type="submit" v-on:click.prevent="login" value="Login" class="btn float-right login_btn">
</div>
</form>
</template>
<script>
export default
name: "Login",
data: () =>
return
email: '',
password: ''
,
methods:
login()
this.$store.dispatch('users/login',, email: this.email, password: this.password)
,
,
</script>
js/store/modules/users.js
export default
namespaced: true,
state:
user:
,
getters:
user(state)
return state.user
,
mutations:
user(state, user)
state.comment = user
,
actions:
login(emailData,passwordData)
axios.post('/login',
'email': emailData, 'password': passwordData
).then(response =>
if (response.status === 201)
console.log('login')
else
console.log(response.data)
).catch(error =>
console.log('oops')
);
,
js/store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
import comments from "./modules/comments";
import users from "./modules/users";
Vue.use(Vuex);
export default new Vuex.Store(
modules:
comments,
users
)
js/app.js
import './bootstrap'
import Vue from 'vue'
import router from './routes/index'
import App from './components/App.vue';
import store from './store/index'
window.Vue = Vue
const app = new Vue(
el:'#app',
router,
store,
render: h => h(App)
);
我怀疑我的问题在于对 js 语法的理解不正确,或者将数据作为参数传递给方法 axios.post() 和 this.$store.dispatch() 所需的方式。但无论如何。我不知道该怎么办。在我用blade代替vues的时候,很容易做登录动作,但是用vue js,vuex就遇到了困难。
其中一个错误,从我的 mozilla 浏览器复制而来
"message": "Array to string conversion",
"exception": "ErrorException",
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\app\\Providers\\FortifyServiceProvider.php",
"line": 40,
"trace": [
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\app\\Providers\\FortifyServiceProvider.php",
"line": 40,
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
,
"function": "App\\Providers\\closure",
"class": "App\\Providers\\FortifyServiceProvider",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\ThrottleRequests.php",
"line": 85,
"function": "call_user_func"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\ThrottleRequests.php",
"line": 55,
"function": "handleRequestUsingNamedLimiter",
"class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php",
"line": 78,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php",
"line": 49,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php",
"line": 121,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php",
"line": 64,
"function": "handleStatefulRequest",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php",
"line": 37,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php",
"line": 67,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Cookie\\Middleware\\EncryptCookies",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 103,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
"line": 697,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
"line": 672,
"function": "runRouteWithinStack",
"class": "Illuminate\\Routing\\Router",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
"line": 636,
"function": "runRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php",
"line": 625,
"function": "dispatchToRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
"line": 166,
"function": "dispatch",
"class": "Illuminate\\Routing\\Router",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 128,
"function": "Illuminate\\Foundation\\Http\\closure",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TrimStrings.php",
"line": 40,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php",
"line": 27,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance.php",
"line": 86,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\fruitcake\\laravel-cors\\src\\HandleCors.php",
"line": 38,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Fruitcake\\Cors\\HandleCors",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Middleware\\TrustProxies.php",
"line": 39,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 167,
"function": "handle",
"class": "Illuminate\\Http\\Middleware\\TrustProxies",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php",
"line": 103,
"function": "Illuminate\\Pipeline\\closure",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
"line": 141,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php",
"line": 110,
"function": "sendRequestThroughRouter",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
,
"file": "C:\\xampp\\htdocs\\dashboard\\test\\social_network\\public\\index.php",
"line": 52,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
]
更新:
这是来自 FortifyServiceProvider.php 的代码
<?php
namespace App\Providers;
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Fortify;
class FortifyServiceProvider extends ServiceProvider
/**
* Register any application services.
*
* @return void
*/
public function register()
//
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
RateLimiter::for('login', function (Request $request)
return Limit::perMinute(10)->by($request->email.$request->ip());
);
RateLimiter::for('two-factor', function (Request $request)
return Limit::perMinute(5)->by($request->session()->get('login.id'));
);
Fortify::loginView(function ()
return view('auth.login');
);
Fortify::registerView(function ()
return view('auth.register');
);
Fortify::twoFactorChallengeView(function ()
return view('auth.two-factor-challenge');
);
Fortify::verifyEmailView(function ()
return view('auth.verify-email');
);
【问题讨论】:
请出示您FortifyServiceProvider
的代码。
@Rwd 更新了问题。
【参考方案1】:
TL;DR
将您的 login
操作更新为:
action(context, payload)
axios.post('/login',
email: payload.email,
password: payload.password
)
.... // The rest of the code.
或者,您可以只使用payload
作为axios
的数据对象:
axios.post('/login', payload)
.then(...)
.catch(...)
然后将您的 dispatch call by removing the empty
` 更新为第二个参数:
this.$store.dispatch('users/login', email: this.email, password: this.password)
我很确定问题出在您的 login
操作上,包括您如何调用它以及如何使用参数。
使用 Vuex 的操作将context
作为第一个参数(带有state
和commit
之类的对象),然后是一个可选的第二个参数,它接收有效负载(您要传递给它的数据) .dispatch
方法将动作的名称作为第一个参数,并将一个可选的有效负载作为第二个参数传递给动作。
所以,基本上你将上下文对象作为电子邮件属性提交,当它到达服务器时将变成一个数组,因此你得到了错误。
【讨论】:
几分钟...我现在就试试!以上是关于Laravel Fortify,Vuex。数组到字符串的转换错误。 FortifyServiceProvider.php 第 40 行。发生了啥事?的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 8 Fortify 登录显示 429 太多请求
使用 Laravel Livewire 和 Laravel Fortify 进行登录
如何在同一个网站的多个地方使用 laravel fortify 进行身份验证
混合清单不存在 - 在 Hostgator 共享主机中部署 laravel 时 Laravel Fortify Jetstream 登录/注册问题