roolith/framework

Roolith PHP 框架

1.4.1 2022-09-26 19:02 UTC

This package is auto-updated.

Last update: 2024-08-26 22:58:24 UTC


README

Roolith PHP 框架。非常简洁且开销小。

安装

composer create-project roolith/framework your_app_name

文档

如果您想使用此框架或有任何需求,可以通过 me@habibhadi.com 联系我。此框架是为了教育目的而开发的!

生成器

php roolith generate controller DemoController
php roolith generate model Product
php roolith generate middleware AuthMiddleware

定义路由

打开 app/Http/routes.php 并根据 文档 定义路由

错误页面

如果没有定义路由,默认情况下它将在 views 文件夹中寻找 404.php

配置

所有应用程序配置都存储在 config/config.php 中,更多详情请参阅 文档

常量

应用程序常量定义在 /constant.php

前端工作流

npm install

然后

npm start

在 npm start 之前打开 gulpfile.js,根据您的需求更新 browsersync 选项。特别地,更改定义的 vhost local.roolith-framework.me

要添加 SCSS 和 JS,请使用 source/scss/app.scsssource/js/app.js

使用

npm run build

进行生产构建。它将创建 min.css 和 min.js 文件。

模型

模型文件位于 app/Models

<?php
namespace App\Models;

class User extends Model
{
    protected $table = 'users';
}

控制器

控制器文件位于 app/Controllers

<?php
namespace App\Controllers;
use App\Models\User;

class WelcomeController extends Controller
{
    public function index()
    {
        $data = [
            'content' => 'Welcome to Roolith framework!',
            'title' => 'Roolith Framework',
        ];

        return $this->view('home', $data);
    }
    
    public function users()
    {
        return User::all();
    }

    public function show($id)
    {
        return User::orm()->find($id);
    }
}

视图

视图文件位于 /views 中,视图文件非常直接,请参阅 文档

让我们保持简单!

在模板中打印路由 URL

<form action="<?= route('welcome.form') ?>" method="post">

请求

Request::input('page');
Request::has('page');
Request::all();
Request::only('page');
Request::only(['page', 'other_param']);
Request::cookie('cookie_name');
Request::url();
Request::fullUrl();
Request::method();
Request::isMethod('POST');

Request::file('photo');
Request::file('photo')->isValid();
Request::file('photo')->upload($destination);
Request::hasFile('photo');

验证器

$validator = new Validator();
$validator->check(
    [
        'name' => 'john',
        'email' => 'me@habibhadi.com',
        'company' => '',
        'age' => 18,
        'url' => 'something!',
    ],
    [
        'name' => Rules::set()->isRequired()->minLength(10)->isArray()->maxLength(20)->notExists(\App\Models\User::class),
        'email' => Rules::set()->isEmail()->isRequired(),
        'company' => Rules::set()->isRequiredIf('age:greater_than:10'),
        'url' => Rules::set()->isUrl(),
        'age' => Rules::set()->isNumeric(),
    ]
);

if ($validator->success()) {
    // do something!
}

清理

Sanitize::param($_GET['param']);
Sanitize::any('untrusted_string<script>alert("a")</script>');
Sanitize::email('something/@bad.com');
Sanitize::string('xss_protect');

数组方法

示例

_::only(['name' => 'hadi', 'age' => 33], 'name');
_::only(['name' => 'hadi', 'age' => 33, 'something' => 'else'], ['name', 'something']);
_::drop([1, 2, 3, 4, 5]);

方法列表 -

  • except
  • chunk
  • compact
  • concat
  • difference
  • drop
  • dropRight
  • dropWhile
  • filter
  • remove
  • findIndex
  • indexOf
  • join
  • last
  • first
  • reverse
  • take
  • takeRight
  • uniq
  • find
  • each
  • contains
  • map
  • isMultidimensional
  • resetKeys
  • order
  • orderBy
  • orderByString
  • random
  • add
  • flat
  • dot
  • exists
  • get
  • has
  • pluck
  • prepend
  • query
  • set

本地化

获取消息

__('errors.required'); // This field is required

设置本地化

Settings::setLang('es');
Settings::getLang();

一旦设置了 es 语言,它将查找 lang/es/message.php。因此,当设置了 es 时,以下代码将输出 -

__('errors.required'); // este campo es requerido

Cookie

设置 Cookie

Storage::setCookie('name', 'value', Carbon::now()->addMonths());

获取 Cookie

Request::cookie('name');

删除 Cookie

Storage::deleteCookie('name');

会话

Storage::setSession('name', 'value');
Storage::deleteSession('name');