s-damian /
Damian PHP 框架的骨架应用程序。
v1.0.14
2024-08-17 08:42 UTC
Requires
- php: ^8.1
- s-damian/damian-php-fw: ^1.0
- swiftmailer/swiftmailer: 6.3.*
- vlucas/phpdotenv: 5.3.*
Requires (Dev)
- guzzlehttp/guzzle: 6.3.*
- phpstan/phpstan: ^1.10
- phpunit/phpunit: 9.5.*
README
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
- http://www.your-domain.com
- http://www.your-domain.com/contact
- http://www.your-domain.com/blog
- http://www.your-domain.com/blog/slug-1
- http://www.your-domain.com/callable-example
- http://www.your-domain.com/sitemap
文档
- 此框架的文档位于
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; });