Skip to content

s-damian/damian-php

Repository files navigation

Tests Static analysis Latest Stable Version License

Damian PHP Framework - Skeleton

A powerful PHP Framework in PHP 8.4 - Beautiful code & Elegant syntax

SGBDR: Compatible with MySQL / MariaDB / PostgreSQL

This Open Source Framework is developed by Stephen Damian

Here you have the source codes of the skeleton.

Kernel source code

The kernel source codes for this Framework are in this package:

Damian PHP Framework - Kernel - [damian-php-fw]

Getting Started

Requirements

  • PHP 8.2 || 8.3 || 8.4

Create a new project

  • You can create a new project via the composer create-project command:
composer create-project s-damian/damian-php example-app-name

Configuration

  • Create your .env file:
cd /your-path/example-app-name
cp .env.example .env
  • You have to configure the .env file.

Configuration - HTTP Server

  • You have to configure your web server (Linux / Nginx or Apache / MySQL or PostgreSQL / PHP).

You have an example Nginx Vhost configuration in docs/nginx/vhost-example.conf file.

After configuring your HTTP server (Nginx), you can run these demo URLs

Documentation

  • The documentation for this Framework is in docs/DamianPhp folder.

Syntax examples

Routing

An example of a route listing:

<?php

Router::group(['namespace' => 'Front\\', 'prefix' => 'website'], function () {
    Router::post(
        '/contact',
        'Contact@sendMail',
        ['name' => 'contact_send-mail']
    );
    Router::group(['prefix' => '/blog'], function () {
        Router::get(
            '',
            'Article@index',
            ['name' => 'article_index']
        );
        Router::get(
            '/{slug}',
            'Article@show',
            ['name' => 'article_show']
        );
    });
});

Retrieve a URL with the name of a route:

<?php echo route('article_show', ['slug' => $article->slug]); ?>

ORM (compatible with MySQL / MariaDB and PostgreSQL)

Active Record Pattern

Example to insert an article (using the setters magic methods):

<?php

$article = new Article();
$article->setTitle('Article 1');
$article->setDescription('Description');
$article->setContent('Content');
$article->setSlug('slug-1');
$article->save();

Example to update an article (using the fill magic method):

<?php

$article = Article::load()->findOrFail($id);
$article->fill(Request::getPost()->all());
$article->save();

Fetch multiple rows

Example using the when magic method:

<?php

$articles = Article::load()
    ->select('title, description, content')
    ->where('slug', '!=', 'article-2')
    ->when((int) Input::get('status') === 1, function ($query) {
        return $query->where('status', '=', 1);
    })
    ->findAll();

ORM with Pagination

To paginate an item listing:

<?php

$article = new Article();

$articles = $article->where('status', '=', 1)->paginate(20);

$pagination = $article->getPagination();

foreach ($articles as $article) {
    echo $article->title;
}

echo $pagination->render();
echo $pagination->perPageForm();

Pagination

<?php

$pagination = new Pagination();

$pagination->paginate($countElements);

$limit = $pagination->getLimit();
$offset = $pagination->getOffset();

// Here your list of items with a loop.

echo $pagination->render();
echo $pagination->perPageForm();

Validation

Validation example (you can do method injection):

<?php

public function update(Validator $validator, int $id)
{
    $validator->rules([ // Add your rules in the array.
        'title' => ['max' => 190, 'required' => true],
        'description' => ['max' => 190, 'required' => true],
        'content' => ['required' => true],
    ]);

    if ($validator->isValid()) {
        // Success
    } else {
        // Error
        $validator->getErrorsHtml();
    }
}

You can add custom validation rules. Example:

<?php

Validator::extend('strictly_equal', function ($input_name, $input_value, $parameters) {
    return (string) $input_value === (string) $parameters;
});