A mini PHP framework built from scratch following the latest PSR standards.
The framework includes concepts like:
routes.php
You can make GET, POST, PUT, DELETE, or any HTTP route.
// GET
$app->get('users', ['UsersController', 'index']);
// POST
$app->post('users', ['UsersController', 'store']);
You can return HTML views, JSON, or any other content type either from Controllers or by using anonymoues functions.
# return html view
$app->get('/', ['PagesController', 'home']);
# return json
$app->get('users', ['UsersController', 'index']);
# return plain text
$app->get('status', function ($app) {
return '<pre> I\'m OK </pre>';
});
The app container file is located at app/Core/Container.php
.
You can bind items to the app within bootstrap/app.php
using the following array syntax.
$app->bind('databaseConnection', [new App\Core\Database\Connection, 'make']);
The first item in the array is the instance of the class you want to bind and the second is the method to invoke.
And to simply resolve items out of the container:
$db = $app->databaseConnection;
Checkout app/Core/App.php
and app/Core/Database/Connection.php
to understand that the container object is automatically injected into its bindings. Here the binding is databaseConnection
.
class Connection
{
/*
* App\Core\Container $container is injected
* automatically by $app (bind)
*/
public function make($container)
{
//
}
}
The following code within app/Core/Database/QueryBuilder.php
shows that we're injecting the PDO dependency into the Connection
class.
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
Apart from the array syntax, you can also bind items to the app by passing a closure as the second argument, within bootstrap/app.php
$app->bind('database', function ($app) {
return new App\Core\Database\QueryBuilder($app->databaseConnection);
});
The framework includes many other concepts like MVC, front-controller pattern, collections, models, custom config file, etc.
Simply clone and install by:
composer install
And bootup the server by running:
php -S localhost:8080 -t public/
Note that the framework is not to be used for real projects. Built to understand the inner workings of popular frameworks in a simplistic way!