Damian PHP 框架的骨架应用程序。

安装: 4

依赖项: 0

建议: 0

安全性: 0

星标: 3

关注者: 2

分支: 0

开放性问题: 0

类型:项目

v1.0.14 2024-08-17 08:42 UTC

This package is auto-updated.

Last update: 2024-09-17 08:50:43 UTC


README

Tests Static analysis Latest Stable Version License

Damian PHP 框架 - 骨架

强大的PHP框架,在 PHP 8.3 - 美观代码 & 精巧语法

SGBDR:兼容 MySQL / MariaDB / PostgreSQL

开源框架Stephen Damian 开发。

这里是 skeleton 的源代码。

内核源代码

此框架的内核源代码包含在这个包中。

Damian PHP 框架 - 内核 - [damian-php-fw]

入门指南

要求

  • PHP 8.1 || 8.2 || 8.3

创建新项目

  • 您可以通过 composer create-project 命令创建新项目。
composer create-project s-damian/damian-php example-app-name

配置

  • 创建您的 .env 文件
cd /your-path/example-app-name
cp .env.example .env
  • 您必须配置 .env 文件。

配置 - HTTP 服务器

  • 您必须配置您的 web 服务器(Linux / Nginx 或 Apache / MySQL 或 PostgreSQL / PHP)。

您可以在 docs/nginx/vhost-example.conf 文件中找到 Nginx Vhost 配置示例。

配置 HTTP 服务器(Nginx)后,您可以运行以下演示 URL

文档

  • 此框架的文档位于 docs/DamianPhp 文件夹中。

语法示例

路由

路由列表示例

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

通过路由名称检索 URL

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

ORM(兼容 MySQL / MariaDB 和 PostgreSQL)

Active Record 模式

插入文章的示例(使用 setters 魔法方法)

<?php

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

更新文章的示例(使用 fill 魔法方法)

<?php

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

检索多行

使用 when 魔法方法的示例

<?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 分页

分页项目列表

<?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();

分页

<?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();

验证

验证示例(您可以进行方法注入)

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

您可以添加自定义验证规则。示例

<?php

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