Last active
July 8, 2016 06:51
-
-
Save mozmorris/4b9e30d5a2f3165011a0 to your computer and use it in GitHub Desktop.
example php app using php's built-in server (npm run backend)
This file contains hidden or 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
{ | |
"name": "MozMorris/phpapp", | |
"require": { | |
"slim/views": "~0.1.3", | |
"twig/twig": "~1.0" | |
} | |
} |
This file contains hidden or 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
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Hello World</title> | |
</head> | |
<body> | |
<!-- app/views/index.html --> | |
<p> | |
Hello {{message}} | |
</p> | |
</body> | |
</html> |
This file contains hidden or 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 | |
// htdocs/index.php | |
require dirname(__FILE__) . '/../vendor/autoload.php'; | |
/** | |
* App Setup | |
*/ | |
$app = new \Slim\Slim(array( | |
'debug' => preg_match('/^(\d+\.\d+\.\d+\.\d+|localhost)/', $_SERVER['HTTP_HOST']) ? true : false, | |
'view' => new \Slim\Views\Twig(), | |
'templates.path' => dirname(__FILE__) . '/../app/views' | |
)); | |
// Twig templating options | |
$app->view()->parserOptions = array( | |
'debug' => $app->config('debug'), | |
'cache' => dirname(__FILE__) . '/../app/cache' | |
); | |
/** | |
* Home | |
*/ | |
$app->get('/', function () use ($app) | |
{ | |
$app->render('index.html', array('message' => 'world!')); | |
}); | |
/** | |
* Dispatch | |
*/ | |
$app->run(); |
This file contains hidden or 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
{ | |
"name": "phpapp", | |
"version": "0.0.0", | |
"description": "example php app using phps built-in server", | |
"main": "htdocs/bundle.js", | |
"scripts": { | |
"backend": "php -S 0.0.0.0:5000 server.php" | |
}, | |
"author": "", | |
"license": "MIT" | |
} |
This file contains hidden or 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 | |
// server.php | |
$uri = urldecode( | |
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) | |
); | |
if ($uri !== '/' && file_exists(__DIR__ . '/htdocs' . $uri)) | |
{ | |
return false; | |
} | |
$_SERVER['SCRIPT_NAME'] = 'index.php'; | |
require_once __DIR__ . '/htdocs/' . $_SERVER['SCRIPT_NAME']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment