gpt4 book ai didi

laravel - 重定向回输入在 laravel 5.5 中不起作用

转载 作者:行者123 更新时间:2023-12-05 02:16:17 24 4
gpt4 key购买 nike

我以前多次使用带输入的重定向返回。但是在这个项目中我无法这样做。这是我处理表单请求的 Controller 方法:

public function verifyMobileCode( Request $request)
{
$userId = Auth::user()->id;
if( Auth::user()->verification_code == $request['verification_code'])
{
User::where('id', $userId)->update(['verified'=>1]);
return redirect('/')->with('success', 'Account verified.');
}
else
{
return redirect()->back()->withErrors('verification_code' ,'unv' )->withInput($request->all());
}
}

这是我的表单 Blade :

@extends('layouts.index')

@section('content')

<div class="container" style='padding-top: 150px;'>

<?php var_dump($errors) ; ?>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Verify your mobile</div>

<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('verifyMobileCode') }}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('verification_code') ? ' has-error' : '' }}">
<label for="verification_code" class="col-md-4 control-label">Verification code</label>

<div class="col-md-6">
<input id="verification_code" type="text" class="form-control" name="verification_code" value="{!! old('verification_code') !!}" required autofocus maxlength="6" pattern="\d{6}">

@if ($errors->has('verification_code'))
<span class="help-block">
<strong>Please enter 6 digit number sent to your mobile.</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

我的Kernel.php如下:

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\LanguageSwitcher::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
];

/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
'throttle:60,1',
'bindings',
],
];

/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'is-admin' => \App\Http\Middleware\IsAdminMiddleware::class,
];
}

我没有发现任何错误或错误。你有看到什么吗?是否有任何调试此行为的技术?提前致谢。

最佳答案

我的方法是使用 FormRequest .

php artisan make:request VerifyCodeRequest

请求正文:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class VerifyCodeRequest extends FormRequest
{

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}


/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'verification_code' => 'required|in:'.$this->user()->verification_code, // since user is already logged in we check if verification_code matches using `in` rule
];
}


public function messages()
{
return [
'in' => 'Your custom error message if we failed',
];
}
}

现在您将 verifyMobileCode(...) 的签名更改为以下 verifyMobileCode(App\Http\Requests\VerifyCodeRequest $request)

现在,代码仅当我们通过验证时才执行函数体,因此我们需要更新用户实例,我们就完成了。

public function verifyMobileCode(App\Http\Requests\VerifyCodeRequest $request)
{
$request->user()->verify();
return redirect('/')->with('success', 'Account verified.');
}

在 User 模型中添加 verify() 函数

public function verify() 
{
$this->update(['verified' => true]);
}

如果您的上述方法不起作用,则说明您的 app/Http/Kernel.php 存在问题。你用过

\Illuminate\Session\Middleware\StartSession::class,

两次。你必须删除一个。可能是第一个。

查看库存 Laravel 的 kernel.php .

关于laravel - 重定向回输入在 laravel 5.5 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50230038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com