roots / docs

📝 Documentation for Roots projects

Home Page:https://roots.io/docs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Document how to override Acorn kernel / boot

stefanfisk opened this issue · comments

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

commented

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. 👍

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.

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.