PatrickLouys / no-framework-tutorial

A small tutorial to show how to create a PHP application without a framework.
MIT License
1.53k stars 186 forks source link

todo #30

Closed PatrickLouys closed 6 years ago

PatrickLouys commented 9 years ago

revisit the "routing" and "dynamic pages" chapters and then do a chapter on DB handling

phillipsharring commented 6 years ago

Not sure about the routing, but the DB dynamic pages part could be a new PageReader implementation with a simple PDO object passed in (which could obviously be an implementation and concrete...) Or abstracted classes for the various tables... Yeah, the rabbit hole goes deep. But the way you build on things from chapter to chapter I feel like it could be started with a simple implementation and piled on later.

<?php

namespace Example\Page;

// use stuff ...
use Example\Database\Database; // an interface and the injector gets PdoDatabase or something...

class DbPageReader implements PageReader
{
    private $db;

    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    public function readBySlug(string $slug) : string
    {
        $page = $this->db->row('SELECT * FROM pages WHERE slug = ?', ['slug' => $slug]);

        if (null == $page) {
            throw new InvalidPageException($slug);
        }

        return $page->body;
        // or whatever...
    }
}

I've been playing with it here: philsown/noframework and I'll link up any commits that are relevant...

ellisgl commented 6 years ago

I've been using "my own" wrapper: https://github.com/ellisgl/GeekLab-GLPDO

The structure I use is as follows:

src\
    Db\
        Connect\
            {databasename}.php
        Model\
            ---Models are data that are manipulated---
        Query\ ---Query files only have single queries---
           {tablename}.php