gpt4 book ai didi

php - Laravel-5 在表单请求的 authorize() 函数中重定向

转载 作者:搜寻专家 更新时间:2023-10-31 20:37:41 26 4
gpt4 key购买 nike

我是否可以根据请求从 authorize() 函数中创建重定向?我试过下面的代码,但它不满足重定向请求。任何人都可以阐明这一点吗?

谢谢。

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use App\Reserve;
use Cookie;
use Config;

class ClassVoucherCheckoutRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(Reserve $reserve, Cookie $cookie)
{
if((!$cookie->has(Config::get('app.cookie_name'))) || ($reserve->where('cookie_id', $cookie->get(Config::get('app.cookie_name')))->count() == 0))
{
return redirect()->to('butchery-voucher')->withErrors('Your reservation has expired. Places can only be held for up to 30 minutes.');
}

return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [

];
}
}

最佳答案

我也有同样的问题,我还没有找到任何解决方案,但我已经通过另一种方式解决了这个问题,我知道这不是正确的解决方案,但现在可能会有帮助。

我的问题是:如果数据库中不存在具有相同 fb_id 的任何其他用户,我需要注册一个用户。但我无法检查这种情况,因为中间件在 Controller 之前执行,它返回给我 fb_id already taken error。

这是我的UserController:

public function createUser (UserRequest $request) {

/** here I need to redirect user if the given `fb_id` is already exists
before it was always returning the `fb_id` exists error before executing
the following code, because all input filtered by the `UserRequest` middleware
I have changed the `UserRequest.php` to execute the following code.
**/
$fb_id = Input::get('fb_id');
$user = $this->user->getUserWhereFbIdIn([$fb_id]);

if(sizeof($user) > 0){
return Response::json(['result' => true, 'error' => false, 'message' => 'User exists', 'data' => $user]);
}


// insert user code is here
}

用户请求.php:

public function authorize()
{
return true;
}


public function rules()
{
$fb_id = Input::get('fb_id');
$user = User::where('fb_id', $fb_id)->get()->toArray();

if(sizeof($user) > 0){
return [];
}
return [
'fb_id' => 'required|unique:users',
'username' => 'required|unique:users',
'email' => 'required|unique:users',
'image' => 'required',
'device_id' => 'required',
'status' => 'required',
];
}

关于php - Laravel-5 在表单请求的 authorize() 函数中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31067138/

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