Coder Social home page Coder Social logo

php-mini-framework's Introduction

Intro

A mini PHP framework built from scratch following the latest PSR standards.

The framework includes concepts like:

Routing

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']);

Responses

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>';
});

Container

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;

Dependency Injection

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;
}

Closures

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);
});

And more

The framework includes many other concepts like MVC, front-controller pattern, collections, models, custom config file, etc.

Installation

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.