【问题标题】:Boot entire Laravel5 application from outside从外部启动整个 Laravel5 应用程序
【发布时间】:2018-07-11 13:44:46
【问题描述】:

我正在尝试从外部 php 脚本启动整个 L5 应用程序。我需要访问 Eloquent 模型和外观。例如,我在不同的文件夹中安装了两个正在运行的应用程序:

  • /var/www/drupal7
  • /var/www/drupal7/laravel

我的 nginx 虚拟主机配置:

location /laravel {
      root   /var/www/drupal7/laravel/public;
      index index.php;
      try_files $uri $uri/ /laravel/public/index.php?$query_string;
}

我的 /var/www/drupal7/laravel/routes/web.php

Route::get('/laravel', function () {
    return 'Laravel';
});

两个应用程序都运行良好,但我需要从外部脚本启动 laravel 应用程序,并且我想访问模型、视图和配置。 我的问题是 - 我怎样才能正确地做到这一点?

我尝试使用以下代码实现此目的:

<?php

require '/var/www/drupal7/laravel/bootstrap/autoload.php';

$app = new Illuminate\Foundation\Application(
    '/var/www/drupal7/laravel/'
);

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

use App\User;

$user = User::find(1);
var_dump($user);

但我收到以下错误消息:

Error: Call to a member function connection() on null in Illuminate\Database\Eloquent\Model::resolveConnection() (line 1013 of /var/www/drupal7/laravel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php)

【问题讨论】:

  • 查看 public/index.php 以了解框架如何为请求启动自身。从命令行查看 artisan.php 以了解这是如何发生的。
  • 如果我使用这些文件中的方法,我会收到以下错误消息:FatalErrorException in Router.php line 314: Illuminate\Routing\Router::loadRoutes(): Failed opening required '/' (include_path='.:/usr/share/php')

标签: php laravel initialization bootstrapping


【解决方案1】:

试试这个

<?php
// boot laravel
require __DIR__ .'/../../vendor/autoload.php';
$laravel_app = require __DIR__ .'/../../bootstrap/start.php';
$laravel_app->boot();

this article for the solution that helped me

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多