简体   繁体   English

如何从 Laravel 应用程序中引导 Laravel 应用程序?

[英]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.

相关问题 如何从另一个Laravel应用程序中捕获Laravel错误? - How can I catch a Laravel error from another Laravel application? 如何部署Laravel 4应用程序进行生产? - How can I deploy a Laravel 4 application for production? wordpress 应用程序中的 Laravel - Laravel within a wordpress application 如何处理从 php laravel 应用程序路由中的 API 端点(django 应用程序)返回的数据 - How can I handle data returned from an API endpoint (django application) in a php laravel application route 如何从Ubuntu的Laravel应用程序访问Windows服务器中的文件? - How to access files within a Windows server from a Laravel application in Ubuntu? Laravel应用程序引导设置选项 - Laravel application bootstrap setting options 如何在另一个Web应用程序中嵌入Laravel应用程序? - How to embed a Laravel application within another web application? 如何使用Laravel Application测试“取消选中”复选框字段? - How can I “uncheck” a checkbox field with Laravel Application testing? 如何更改 Laravel 6 中的应用程序名称(空格) - How can I change the application name(space) in Laravel 6 如何在我的应用程序中集成 laravel 包 - How can i integrate a laravel package in my application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM