在Laravel外使用Laravel外墙 [英] Using Laravel Facades outside Laravel

查看:59
本文介绍了在Laravel外使用Laravel外墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Laravel应用程序,我将其用作Joomla内置的更大应用程序的API.我真的很喜欢使用Laravel,并决定在Joomla应用程序中使用Eloquent.我通过在Laravel应用程序中导入bootstrap\autoload.php文件并创建Capsule

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();

这很好用,我可以使用Eloquent.它直接从应用程序加载Eloquent模型.

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

我想知道的是如何在我的Joomla应用程序中使用Laravel应用程序的其余部分,包括使用Facades.

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

例如,我在一个Eloquent模型中使用了Config.get('key'),在Laravel中调用时效果很好,但是在Joomla中调用时抛出错误.

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

推荐答案

我查看了laravel_site/public/index.php来查看它如何启动应用程序,到目前为止,看来这是一个可行的解决方案:

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);

外墙现在看起来工作正常.我特意省略了$response->send();$kernel->terminate($request, $response);,以便不会发生路由并覆盖Joomla自己的路由.

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.

我也不再需要实例化Capsule,就像Laravel现在为我做的那样.

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.

这篇关于在Laravel外使用Laravel外墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆