image

A micro-framework for rapid application development.

Table of contents

Installation

To install, simply run:

composer require raekw0n/danielle

Application Structure

.
├── src                     # PHP source code (The App namespace)
│   ├── Extensions          # Core extensions
│   ├── Factories           # Core factories
│   ├── Handlers            # Core handlers
│   ├── Helpers             # Core helper functions
│   ├── Middleware          # Core middleware
│   ├── Models              # Core model
│   ├── Providers           # Core providers
│   ├── Traits              # Core traits
├── composer.json           # Composer dependencies
├── LICENSE                 # The license
└── README.md               # This file

Documentation

Getting Started

Application Bootstrapping

As you’ll read below, Danielle implements the service provider design pattern, using a core service container to load and inject dependencies into the application. If you’re unsure of what this means, I would recommend reading all of the documentation below first, otherwise, to get started, here is an example of bootstrapping an MVC application built using this framework:

<?php

require_once __DIR__.'/../vendor/autoload.php';

Dotenv\Dotenv::create(__DIR__ . '/../')->load();

$app = new Pimple\Container();

$app->register(new Danielle\Providers\LogServiceProvider());
$app->register(new Danielle\Providers\DatabaseServiceProvider());
$app->register(new Danielle\Providers\RouteServiceProvider());
$app->register(new Danielle\Providers\RouteServiceProvider());

require_once __DIR__.'/../config/controllers.php';
require_once __DIR__.'/../config/routes.php';

For a more detailed example, please feel free to check out this skeleton application.

Services

Danielle uses Pimple to define application services. Services are registered via service providers which must extend Pimple’s ServiceProviderInterface, for example, to register a custom logger service:

LogServiceProvider.php:

<?php

namespace App\Providers;

use Monolog\Logger;
use Pimple\Container;
use Monolog\Handler\StreamHandler;
use Pimple\ServiceProviderInterface;

/**
 * Class LogServiceProvider
 */
class LogServiceProvider implements ServiceProviderInterface
{
    /**
     * Register log service provider.
     *
     * @param Container $container
     * @return Container|string
     */
    public function register(Container $container)
    {
        $container['log'] = new Logger('app');
        $container['log']->pushHandler(new StreamHandler($this->logPath(), Logger::DEBUG));

        return $container;
    }

    /**
     * Resolve log path.
     *
     * @return string
     */
    private function logPath()
    {
        return env('LOG_PATH');
    }
}

bootstrap.php:

$app = new Pimple\Container();

$app->register(new App\Providers\LogServiceProvider());

Core

Database

A lightweight PDO wrapper is provided for working with MySQL databases. Simply register the core database service provider in your application’s bootstrap file:

$app->register(Danielle\Providers\DatabaseServiceProvider);

Then define a model which extends from the core model, for example:

<?php
namespace App\Models;

use Danielle\Models\Model;

/**
 * Class Blog
 */
class Blog extends Model
{
    /** @var $title */
    protected $title;
    ...

    /**
     * Get blog post.
     *
     * @return mixed
     */
    public function insertBlogPost()
    {
        $this->db->query('INSERT INTO blog..');
        $this->db->bind(':title', $this->title);
        ...
        return $this->db->execute();
    }

The core model injects the database instance from the service container, so you are free to override the underlying class and use something else other than the lightweight PDO wrapper that is provided by default. If you would like to use something else, you just need to register your custom database service provider in your application’s bootstrap file:

$app->register(App\Providers\MyCustomDatabaseServiceProvider)

Routing

Routing is handled using Klein, which is loaded using the core RouteServiceProvider. If you would like to use something else other than Klein, simply replace the core RouteServiceProvider in your application’s bootstrap file:

$app->register(App\Providers\MyCustomRouteServiceProvider);

Views

View template rendering is handled using Twig , which is loaded using the core ViewServiceProvider and extended using a couple of core extensions. If you would like to use something else other than Twig, just replace the core ViewServiceProvider in your application’s bootstrap file:

$app->register(App\Providers\MyCustomViewServiceProvider);

Extensions

AssetExtension

The core AssetExtension is used to generate relative links to application assets, for example:

<link rel="stylesheet" href="{{ asset({file: 'app.min.css'}) }}" />

Will generate a relative link for app.min.css to your public css folder.

DotenvExtension

The core DotEnvExtension is used to load environment variables defined in your application’s .env file in order to use them in views, for example:

{{ env('APP_NAME') }}

Will output the APP_NAME environment variable defined in .env.

License

danielle is open source software licensed under the MIT license.