Last active
May 5, 2016 09:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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