php - How can I bootstrap a Laravel application from within a Laravel application?
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?
Answer
Solution:
The problem was caused by de namespacing.
Should be
This is also the case for