Giter VIP home page Giter VIP logo

frameworkless's Introduction

Frameworkless

An admittedly overly simplistic example of combining a few popular packages into your own micro-framework.

Why?

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.

What's included?

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:

Getting started with Vagrant

Prerequisites

Steps

  1. git clone this repository
  2. cd into the cloned repository
  3. composer install
  4. cp misc/.env.example .env
  5. cp misc/Vagrantfile .
  6. vagrant up

From here, you should be able to access http://localhost:8080/. This website is served using NGINX & PHP 7.

Getting started with Docker

Prerequisites

Steps

  1. git clone this repository
  2. cd into the cloned repository
  3. composer install
  4. cp misc/.env.example .env
  5. cp misc/Dockerfile .
  6. cp misc/docker-compose.yml .
  7. docker-compose up

Run docker-compose port application 80 to find the public port this application is running on.

Batteries not included

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 :).

PDO (database)

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

Spot (database, ORM)

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

Contributing

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

frameworkless's People

Contributors

mmeyer724 avatar waxim avatar jaakkytt avatar

Watchers

James Cloos avatar  avatar

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.