What Are "Requests" in Laravel?

Bruce Gust
Bruce Gust used Ask the Experts™
on
I'm working through a Laravel tutorial and it references "Requests." I followed along and I created a "CreatePage.php" file in a brand new "Requests" folder and it looks like this:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreatePage 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 [
            'name' => 'required', 
			'body' => 'required'
        ];
    }
}

Open in new window


Does this need to be cited in a Controller or a Model? I ask because the "rules" seem to imply that if my user doesn't include a value for either the name of the body columns, then they're going to get an error. That error doesn't show up.

What are "requests" and how do they need to be referenced so the rules actually apply?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fractional CTO
Distinguished Expert 2018
Commented:
https://laravel.com/docs/4.2/requests suggests a Laravel "Request" relates to any Web request, independent of verb used...

So GET/PUT/POST... whatever... are all HTTPS + Laravel requests.
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018
Commented:
In the most of time, developers need to fetch an instance of the ongoing HTTP request. In Laravel, we can obtain so by using Illuminate\Http\Request class in our controller. This will automatically fetch the incoming HTTP request instance.

Sample :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Update the specified user.
     *
     * @param  Request  $request
     * @param  string  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }
}

Open in new window


"HTTP Requests" Official Doc
Bruce GustPHP Developer

Author

Commented:
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial