An admittedly overly simplistic example of combining a few popular packages into your own micro-framework.
Education.
I do not recommend building your own framework unless you have a very compelling reason to do so. Instead,use a popular & well-supported framework like Symfony, Slim, or Laravel.
I spent some time picking out packages, preferring those used by existing large applications or frameworks. Here is what's included, in no particular order:
- nikic/fast-route
- Popular routing library used by frameworks like Slim.
- filp/whoops
- Stunning error handler, it makes errors sting a bit less.
- symfony/http-foundation
- Simplifies request & reponse handling.
- league/container
- Dependency injection container, for sharing common resources (like a database connection).
- twig/twig
- The rock solid templating engine used by Symfony and many others.
- vlucas/phpdotenv
- Please don't push your database credentials to GitHub.
git clone
this repositorycd
into the cloned repositorycomposer install
cp misc/.env.example .env
cp misc/Vagrantfile .
vagrant up
From here, you should be able to access http://localhost:8080/. This website is served using NGINX & PHP 7.
git clone
this repositorycd
into the cloned repositorycomposer install
cp misc/.env.example .env
cp misc/Dockerfile .
cp misc/docker-compose.yml .
docker-compose up
Run docker-compose port application 80
to find the public port this application is running on.
I've intentionally made this project as simplistic as possible. A lot of things are left up to you to design and implement. On the plus side, you won't have to remove much boilerplate.
Below you will find instructions on how to implement a few things, feel free to contribute more examples :).
Edit bootstrap/app.php
and add the following:
$container
->add('PDO')
->withArgument(getenv('DB_CONN'))
->withArgument(getenv('DB_USER'))
->withArgument(getenv('DB_PASS'));
You will also need to add some values to your .env
# Database access
DB_CONN=mysql:host=127.0.0.1;dbname=frameworkless;charset=utf8
DB_USER=fwl_user
DB_PASS=hopefullysecure
Now, from a controller:
private $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function get()
{
$handle = $this->pdo->prepare('SELECT * FROM `todos`');
$handle->execute();
return new JsonResponse($handle->fetchAll(PDO::FETCH_ASSOC));
}
composer require vlucas/spot2
from here, edit bootstrap/app.php
and add the following:
$db = new \Spot\Config();
$db->addConnection('mysql', [
'dbname' => getenv('DB_NAME'),
'user' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'host' => getenv('DB_HOST')
]);
$container
->add('\Spot\Locator')
->withArgument($db);
You will also need to add some values to your .env
# Database access
DB_CONN=mysql:host=127.0.0.1;dbname=frameworkless;charset=utf8
DB_USER=fwl_user
DB_PASS=hopefullysecure
Now you can create models! I recommend adding them under a src/Models directory for separation. For example, src/Models/Posts.php
:
namespace Frameworkless\Models;
use Spot\Entity;
class Posts extends Entity
{
protected static $table = 'posts';
// etc.
}
And finally from your controller:
private $spot;
public function __construct(\Spot\Locator $spot)
{
$this->spot = $spot;
}
public function get()
{
$posts = $this->spot->mapper('Frameworkless\Models\Posts')->all();
return new Response('Here are your posts ' . print_r($posts, true));
}
Submit a pull request :) I'll be friendly
Thanks to @waxim for contributing the Spot example
Thanks to @jaakkytt for clearing up part of this readme
Thanks to @Luciam91 for contributing Docker support