Skip to content

Instantly share code, notes, and snippets.

@mozmorris
Last active July 8, 2016 06:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mozmorris/4b9e30d5a2f3165011a0 to your computer and use it in GitHub Desktop.
Save mozmorris/4b9e30d5a2f3165011a0 to your computer and use it in GitHub Desktop.
example php app using php's built-in server (npm run backend)
{
"name": "MozMorris/phpapp",
"require": {
"slim/views": "~0.1.3",
"twig/twig": "~1.0"
}
}
<html>
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<!-- app/views/index.html -->
<p>
Hello {{message}}
</p>
</body>
</html>
<?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();
{
"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"
}
<?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