Using Laravel Facades outside Laravel

蹲街弑〆低调 提交于 2019-12-23 15:36:50

问题


I have a Laravel application which I use as an API to a much larger application built in Joomla. I really love using Laravel and decided to use Eloquent within the Joomla application. I got this working by importing the bootstrap\autoload.php file in the Laravel application and creating a Capsule

require JPATH_ROOT.'/../laravel_app/bootstrap/autoload.php';

$capsule = new Capsule();
$config = new JConfig();

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => $config->host,
    'database'  => $config->db,
    'username'  => $config->user,
    'password'  => $config->password,
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => $config->dbprefix,
    'strict'    => false
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();

This works great and I can use Eloquent. It loads the Eloquent models from the app directly.

What I want to know is how I get the rest of the Laravel app working inside my Joomla app, including using Facades.

For example I have the use of Config.get('key') within one of the Eloquent models, works fine when called in Laravel, but throws and error when called in Joomla.

Fatal error: Class 'Config' not found in laravel_app/app/Model.php on line 192

回答1:


I have looked at laravel_site/public/index.php to see how it initiates the application and so far so good this seems to be a working solution:

require JPATH_ROOT.'/../laravel_site/bootstrap/autoload.php';
$app = require_once JPATH_ROOT.'/../laravel_site/bootstrap/app.php';

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

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

//$response->send();

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

Facades now appear to be working fine. I've purposely left out $response->send(); and $kernel->terminate($request, $response); so that the routing does not take place and override Joomla's own routing.

I also no longer need to instantiate the Capsule as Laravel is doing that for me now.

I haven't tested this fully so I do not know how robust it is or what features will work but all is good so far.



来源:https://stackoverflow.com/questions/32722677/using-laravel-facades-outside-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!