DEV Community

Cover image for Roll your own Request object in Laravel
Titas Gailius
Titas Gailius

Posted on

6 1

Roll your own Request object in Laravel

There are multiple ways to extend or modify the Request object in Laravel. I'd like to show you a method, which in my opinion is the cleanest one.


Macro

You may already know about the macro method. It's the most obvious way to introduce new methods to the Request object but it has some downsides.

Request::macro('project', function () {
    return $this->user();
});
Enter fullscreen mode Exit fullscreen mode

This solution is simple but it has some flaws:

• You cannot override or change existing methods.
• It's not obvious where these new methods are coming from.
• No IDE autocompletion.


Custom Request Object

I like creating my own Request object.

<?php

namespace App\Http;

use Illuminate\Http\Request as LaravelRequest;

class Request extends LaravelRequest
{
    /**
     * Get the team making the request.
     *
     * @param  string|null  $guard
     * @return mixed
     */
    public function team($guard = null)
    {
        return $this->user($guard);
    }
}
Enter fullscreen mode Exit fullscreen mode

It's simple, clean and straightforward. Much better than introducing new methods via "macros".


Now, we need to instruct Laravel to use this new custom class as a base.

Simply override the handle method in our App\Http\Kernel class to use your custom Request object.

<?php

namespace App\Http;

use App\Http\Request;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    // ...

    /**
     * Handle an incoming HTTP request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function handle($request)
    {
        return parent::handle(
            Request::createFrom($request)
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

... and finally, alias your new App\Http\Request class so the container always returns the same instance.

$this->app->alias('request', Request::class);
Enter fullscreen mode Exit fullscreen mode

Happy Coding!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
robertkeli profile image
Robert Ndung'u

This is absolutely fantastic. Been looking for a way to do this, and the macro method wasn't just a clean way. Thanks a tad much @titasgailius

Collapse
 
azharlihan profile image
Azhar Lihan

Awesome. That request alias save me.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay