Laravel-5 redirect in authorize () function on form requests

Is it possible to create a redirect from the authorize () function in the request? I have tried the following code, but it fails the redirect request. Can anyone shed some light on this?

Thank.

<?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 [

        ];
    }
}

      

+3


source to share


2 answers


I have the same problem too, I haven’t found a solution yet, but I do it in a different way, I know this is not the correct solution, but there may be help at the moment.

My problem: I need to register a user if some other user with the same fb_id

does not exist in the database. But I was unable to test this condition because the middelware is executed before the controller and it returns me the already accepted error fb_id

.

This is mine 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',
    ];
}

      

+2


source


I think the most elegant solution is to return authorize()

return false

if you want to redirect and override the method forbiddenResponse()

in the class FormRequest

. The downside is that you either have to execute the state logic twice or set a state variable.

class MyRequest extends FormRequest
{
    public function authorize(): bool
    {
        return Auth::user()->hasNoEmail() ? false : true;
    }

    public function forbiddenResponse(): Response
    {
        if Auth::user()->hasNoEmail() return redirect(route('user.should_provide_email'));

        return parent::forbiddenResponse();
    }

    public function rules(): array
    {
        return [];
    }
}

      



Of course, one could argue that such redirects should always be done in middleware applied to specific route groups, but being able to do this in the Request class might be nice.

0


source







All Articles