编程语言
首页 > 编程语言> > php-Laravel-5在表单请求的authorize()函数内重定向

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

作者:互联网

是否可以根据请求在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的其他任何用户,则需要注册一个用户.但是我无法检查这种情况,因为中间件软件在控制器之前执行,并且返回fb_id已被接受的错误.

这是我的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
    }

UserRequest.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',
    ];
}

标签:laravel,laravel-5,php
来源: https://codeday.me/bug/20191120/2041374.html