[英]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.我正在尝试构建一个概念证明来解决以下问题:我需要将 Kohana 应用程序重构为 Laraval,但我们不断添加新功能并开发应用程序。 So the Kohana and Laravel codebase have to work together for a while.所以 Kohana 和 Laravel 代码库必须协同工作一段时间。
For a proof of concept, I take two Laravel applications where one of them simulates the old Kohana application.为了验证概念,我采用了两个 Laravel 应用程序,其中一个模拟了旧的 Kohana 应用程序。
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.我想到的一个解决方案是在 Laravel 应用程序中创建一个中间件或服务提供者,以检查该路由是否可以在此 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:当我尝试从第一个 Laravel 的中间件类中引导第二个 Laravel 时,出现以下错误:
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?有没有人知道出了什么问题,或者对另一种解决方案有建议?
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.