roots / docs

📝 Documentation for Roots projects
https://roots.io/docs/
168 stars 182 forks source link

Document how to override Acorn kernel / boot #507

Open stefanfisk opened 1 year ago

stefanfisk commented 1 year ago

Terms

Summary

I'd like the possibility to provide my own subclasses for the kernels.

Motivation

Why are we doing this?

To allow configuring stuff like bootstrappers and middleware.

What use cases does it support?

For example, I'd like to add the standard TrimStrings middleware.

What is the expected outcome?

Maybe a filter per kernel type?

Potential conflicts / foreseeable issues

Not sure, I guess it depends on how stable you consider the kernel APIs to be.

Additional Context

No response

QWp6t commented 1 year ago

Maybe I'll check for the existence of \Http\Kernel and \Console\Kernel and load those instead. 🤔

I'll give it a think and come up with a solution. 👍

oskar-koli commented 9 months ago

I needed to add a middleware to the kernel so as a workaround for this issue I was able to override the http kernel singleton in functions.php, right after the bootloader has been booted:

\Roots\bootloader()->boot();

app()->singleton(
    \Illuminate\Contracts\Http\Kernel::class,
    CustomKernel::class
);

Where the CustomKernel extends the Acorn kernel:

namespace App\Core;

use App\Core\Acorn\ViewMiddleware;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Routing\Router;
use Roots\Acorn\Http\Kernel as AcornHttpKernel;

class CustomKernel extends AcornHttpKernel
{

    public function __construct(Application $app, Router $router)
    {
        $this->middlewareGroups["web"][] = ViewMiddleware::class;

        parent::__construct($app, $router);
    }
}

Haven't tested this in production yet, but it seems to work. A proper fix seems to be on it's way, looking at the PRs, but thought I'd post this workaround here in case someone needs it asap.

QWp6t commented 7 months ago

You can pass a callback to Bootloader::boot()

Roots\bootloader()->boot(function ($app) {
    $app->singleton(
        \Illuminate\Contracts\Http\Kernel::class,
        \App\Http\MySuperCoolKernel::class
    );
});

That should be fine for most of you.

If you want to have complete control over the boot process, just bootstrap your kernel, our bootloader will short-circuit.


➡️ Moving this to docs.