本文整理匯總了PHP中Slim\App::run方法的典型用法代碼示例。如果您正苦於以下問題:PHP App::run方法的具體用法?PHP App::run怎麽用?PHP App::run使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Slim\App
的用法示例。
在下文中一共展示了App::run方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __invoke
/**
* @param \swoole_http_request $request
* @param \swoole_http_response $response
* @throws \Exception
*/
public function __invoke($request, $response)
{
$this->app->getContainer()['environment'] = $this->app->getContainer()->factory(function () {
return new Environment($_SERVER);
});
$this->app->getContainer()['request'] = $this->app->getContainer()->factory(function ($container) {
return Request::createFromEnvironment($container['environment']);
});
$this->app->getContainer()['response'] = $this->app->getContainer()->factory(function ($container) {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);
return $response->withProtocolVersion($container->get('settings')['httpVersion']);
});
/**
* @var ResponseInterface $appResponse
*/
$appResponse = $this->app->run(true);
// set http header
foreach ($appResponse->getHeaders() as $key => $value) {
$filter_header = function ($header) {
$filtered = str_replace('-', ' ', $header);
$filtered = ucwords($filtered);
return str_replace(' ', '-', $filtered);
};
$name = $filter_header($key);
foreach ($value as $v) {
$response->header($name, $v);
}
}
// set http status
$response->status($appResponse->getStatusCode());
// send response to browser
if (!$this->isEmptyResponse($appResponse)) {
$body = $appResponse->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$settings = $this->app->getContainer()->get('settings');
$chunkSize = $settings['responseChunkSize'];
$contentLength = $appResponse->getHeaderLine('Content-Length');
if (!$contentLength) {
$contentLength = $body->getSize();
}
$totalChunks = ceil($contentLength / $chunkSize);
$lastChunkSize = $contentLength % $chunkSize;
$currentChunk = 0;
while (!$body->eof() && $currentChunk < $totalChunks) {
if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
$chunkSize = $lastChunkSize;
}
$response->write($body->read($chunkSize));
if (connection_status() != CONNECTION_NORMAL) {
break;
}
}
$response->end();
}
}
示例2: runApp
/**
* @param String $method
* @param Uri $uri
* @param array $post
*/
protected function runApp($method, $uri, $post = [])
{
$this->buildApp();
$this->buildRequest($method, $uri, $post);
$this->app->getContainer()['request'] = $this->request;
$this->app->getContainer()['response'] = $this->response;
$this->response = $this->app->run(true);
$this->response->getBody()->rewind();
$this->html = $this->response->getBody()->getContents();
}
示例3: getResponseToDispatch
private function getResponseToDispatch(ErrorController $errorController, LoggerInterface $logger) : ResponseInterface
{
try {
if (!$this->hasRoutesConfigured()) {
throw new CannotRunWithoutRoutes();
}
return $this->slimApp->run(true);
} catch (\Exception $exception) {
return $this->generateErrorResponse($errorController, $exception, $logger);
}
}
示例4: getActionListeners
/**
* @return ListenerInterface[]
*/
public function getActionListeners()
{
$routeDispatcher = function () {
$postTypeSettings = $this->app->getContainer()->get('postTypes');
foreach ($postTypeSettings as $postType => $settings) {
$this->setRouteForPostTypes($postType, $settings['defaultController']);
}
$this->app->run();
};
return [$this->factory->make('ActionListener', ['names' => 'template_include', 'callable' => $routeDispatcher, 'priority' => 99])];
}
示例5: run
/**
* @inheritdoc
*/
public function run($appNamespace = self::ROOT_NAMESPACE)
{
//
// Each application must exist inside its own namespace. Chubby uses that namespace to search for modules.
$this->appNamespace = $appNamespace;
//
// Slim can be initiated in one of two ways:
// 1. Without a container. Slim will create the default container.
// 2. Receiving a container in the constructor. We can pass Slim some settings and services
// by passing a pre-created container. We do this here via a configuration file.
$container = $this->getContainerConfig();
$this->slim = new \Slim\App($container);
$container = $this->slim->getContainer();
//
$this->modules = \Chubby\PackageLoader::loadModules($container);
if (!is_array($this->modules) || !count($this->modules)) {
throw new \Exception("Chubby Framework requires at least one module.");
}
//
// Initialize the modules following the order given by each module's priority.
foreach ($this->modules as $priority => $modules) {
foreach ($modules as $module) {
$module['object']->setApp($this);
$module['object']->init();
}
}
//
$this->slim->run();
return $this;
}
示例6: __construct
/**
*
* The constructor is called in index.php and sets up the app
*
*/
public function __construct()
{
// we need to change the session_save_path
// due to issues with how data is handled on AWS EC2 instances
$dir = sys_get_temp_dir();
session_save_path($dir);
//session_cache_limiter(false);
//start the session
session_start();
//get settings and instantiate app
$this->app = new App(AppConfig::$slimSettings);
//configure app
$this->setUpDatabase();
$this->setUpRoutes();
//run all the apps!
$this->app->run();
}
示例7: Route
public static function Route()
{
$app = new App();
// Reminder: the request is processed from the bottom up,
// the response is processed from the top down.
$app->add(SlimMiddleware::class);
$app->add(Grover::class);
$app->run();
}
示例8: bootstrap
public function bootstrap()
{
// Load configuration files into ConfigRepository
$config = new ConfigRepository(base_path() . '/config');
// Initialize container for dependency injection
$container = $this->initContainer($config);
// Instantiate Slim
$slim = new Slim($container);
// Load routes into Slim router
$this->initRoutes($config, $container, $slim->router);
// Run app
$slim->run();
}
示例9: dispatchApplication
protected function dispatchApplication(array $server, array $pipe = [])
{
$app = new App();
$app->getContainer()['environment'] = function () use($server) {
return new Environment($server);
};
$middlewareFactory = new PhpDebugBarMiddlewareFactory();
$middleware = $middlewareFactory();
$app->add($middleware);
foreach ($pipe as $pattern => $middleware) {
$app->get($pattern, $middleware);
}
return $app->run(true);
}
示例10: testMiddlewareIsWorkingAndEditorIsSet
public function testMiddlewareIsWorkingAndEditorIsSet()
{
$app = new App(['settings' => ['debug' => true, 'whoops.editor' => 'sublime']]);
$container = $app->getContainer();
$container['environment'] = function () {
return Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET']);
};
$app->get('/foo', function ($req, $res, $args) {
return $res;
});
$app->add(new WhoopsMiddleware());
// Invoke app
$response = $app->run();
// Get added whoops handlers
$handlers = $container['whoops']->getHandlers();
$this->assertEquals(2, count($handlers));
$this->assertEquals('subl://open?url=file://test_path&line=169', $handlers[0]->getEditorHref('test_path', 169));
}
示例11: testIpAllow
/**
* Integration test IpRestrictMiddleware::_invoke() when the given IP is allowed.
*/
public function testIpAllow()
{
// Prepare the Request and the application.
$app = new App();
// Setup a demo environment
$env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.2']);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$headers->set('Accept', 'text/html');
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();
$app->getContainer()['request'] = $req;
$app->getContainer()['response'] = $res;
// Set the options value.
$this->options = ['error_code' => 403, 'exception_message' => 'NOT ALLOWED'];
$app->add(new IpRestrictMiddleware($this->ipSet, false, $this->options));
$appMessage = 'I am In';
$app->get('/foo', function ($req, $res) use($appMessage) {
$res->write($appMessage);
return $res;
});
$resOut = $app->run();
$body = (string) $resOut->getBody();
$this->assertEquals($appMessage, $body, 'The client is allowed to access the application.');
}
示例12: run
/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function run()
{
return $this->slimApp->run();
}
示例13: dirname
<?php
require_once dirname(dirname(__FILE__)) . '/vendor/autoload.php';
use Slim\App;
use Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware;
$app = new App(['settings' => ['debug' => true, 'whoops.editor' => 'sublime']]);
$app->add(new WhoopsMiddleware());
// Throw exception, Named route does not exist for name: hello
$app->get('/', function ($request, $response, $args) {
return $this->router->pathFor('hello');
});
// $app->get('/hello', function($request, $response, $args) {
// $response->write("Hello Slim");
// return $response;
// })->setName('hello');
$app->run();
示例14: run
/**
* Start the application.
*/
public function run()
{
$this->app->run();
}
示例15: testExceptionErrorHandlerDisplaysErrorDetails
/**
* @runInSeparateProcess
*/
public function testExceptionErrorHandlerDisplaysErrorDetails()
{
$app = new App(['settings' => ['displayErrorDetails' => true]]);
// Prepare request and response objects
$env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET']);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();
$app->getContainer()['request'] = $req;
$app->getContainer()['response'] = $res;
$mw = function ($req, $res, $next) {
throw new \Exception('middleware exception');
};
$app->add($mw);
$app->get('/foo', function ($req, $res) {
return $res;
});
$resOut = $app->run();
$this->assertEquals(500, $resOut->getStatusCode());
$this->expectOutputRegex('/.*middleware exception.*/');
}