php - How can I bootstrap a Laravel application from within a Laravel application?

18

I am trying to build a proof of concept to solve the following problem: I need to refactor a Kohana application into Laraval, but we keep adding new features and developing the application. So the Kohana and Laravel codebase have to work together for a while.

For a proof of concept, I take two Laravel applications where one of them simulates the old Kohana application.

A solution I have in mind is to create a Middleware or service provider in the Laravel application that checks if the route could be resolved in this Laravel. In the case it could not resolve the route, the other application should be bootstrapped to execute the request.

When I try to bootstrap the second Laravel from within a middleware class of the first one the following error appears:

Target class [App\Http\Middleware\Illuminate\Contracts\Http\Kernel] does not exist.

On executing the following line:

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

Middleware hanle function:

public function handle($request, Closure $next)
{
    $routes = Route::getRoutes();
    try {
        //route exists
        $routes->match($request);

        return $next($request);
    }
    catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
        //route doesn't exist

        // define('LARAVEL_START', microtime(true));
        require_once env('LARAVEL_FILE_PATH').'/vendor/autoload.php';

        $app = require_once env('LARAVEL_FILE_PATH').'/bootstrap/app.php';

        $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

        $response = $kernel->handle(
            $request = Illuminate\Http\Request::capture()
        );

        $response->send();

        $kernel->terminate($request, $response);

        exit;
    }
}

Does anyone have an idea what is going wrong or maybe have a suggestion for another solution?

585

Answer

Solution:

The problem was caused by de namespacing.

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

Should be

$kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class);

This is also the case for

 $request = Illuminate\Http\Request::capture()

People are also looking for solutions to the problem: php - Query data in database to show posts

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.