Skip to content

Instantly share code, notes, and snippets.

@xxzefgh
Last active May 5, 2016 09:34
Show Gist options
  • Save xxzefgh/96ebf336b5f49211cfacf8a48dda967c to your computer and use it in GitHub Desktop.
Save xxzefgh/96ebf336b5f49211cfacf8a48dda967c to your computer and use it in GitHub Desktop.
Call Laravel routes and get response from pure php. Useful when you have existing project and want to use Laravel to achieve some tasks.
<?php
class LaravelBridge
{
private static $app = null;
private static $kernel = null;
private static function init()
{
if (!self::$app || !self::$kernel) {
require __DIR__ . '/path/to/laravel/bootstrap/autoload.php';
self::$app = require_once __DIR__ . '/path/to/laravel/bootstrap/app.php';
self::$kernel = self::$app->make(Illuminate\Contracts\Http\Kernel::class);
}
}
public static function app()
{
self::init();
return self::$app;
}
public static function sendRequest($route, $method = 'GET', $params = null, $https = false)
{
self::init();
$originalRequest = Illuminate\Http\Request::capture();
$request = $originalRequest->duplicate();
$request->server->set('REQUEST_URI',
$https ? 'https' : 'http' . '://' . $_SERVER['SERVER_NAME'] . $route);
$request->server->set('REQUEST_METHOD', $method);
if ($params != null) {
$request->replace($params);
}
$response = self::$kernel->handle(
$request
);
return $response->content();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment