krāg/karch

一个轻量级且快速的MVC框架,适用于PHP开发者。这个框架的设计与Laravel类似,对于需要比Laravel更轻量级框架的人来说,也易于迁移。

安装: 6

依赖项: 0

建议者: 0

安全: 0

星级: 3

关注者: 1

分支: 0

开放问题: 0

语言:JavaScript

类型:项目

v1.0.3 2023-07-18 04:08 UTC

This package is auto-updated.

Last update: 2024-09-15 08:21:02 UTC


README

KARCH Logo

一个轻量级且快速的MVC框架,适用于PHP开发者。这个框架的设计与Laravel类似,对于需要比Laravel更轻量级框架的人来说,也易于迁移。

我们有一个详细文档,可以帮助您开始使用这个框架,使开始使用框架变得简单。

内容

  1. 安装
  2. 路由
  3. 控制器
  4. 特殊功能
  5. 请求
  6. 数据库查询
  7. 迁移
  8. 错误处理

安装

使用composer,

composer create-project krag/karch example-app

使用git仓库URL从github下载框架,或在您的文件夹中打开终端并运行以下代码。

git clone https://github.com/Kenura-R-Gunarathna/KARCH-Framework.git

路由

路由类的路径包含在routes文件夹中的web.php文件中。

use

use App\Route;

来包含路由类。

Route::post('/login', Controller::class, 'login');

Route::post('/function', function () {
    echo "The anonymous callback function has been executed.";
});

控制器

控制器类的路径包含在routes文件夹中的controller.php文件中。

use

use App\Controller;

来包含控制器类。

public static function login()
{
    // code...
}

特殊功能

视图

要查看HTML或PHP文件,所有视图文件的位置都是相对于Views文件夹给出的。

例如:/index.htmlindex.html(对于位于视图文件夹中的index.html文件)

view("{view-location}");

重定向

要重定向网页,插入

redirect("{full-url}");

在此处应插入网页的完整URL。

资产

要加载公共视图中的图像、文档和视频等资产,插入

asset("{relative-url-from-public-folder}");

在此处应插入位于public文件夹中的文件的相对URL,跳过相对URL中的../public/部分。

路由

返回网站中特定routeurl

route("{route-name-or-uri}");

请求

返回网站的$_REQUEST值。

request("{request-name}");

配置

返回网站中.env文件中的$_ENV值。

config("{config-name}");

获取

返回网站的$_GET请求值。

get("{get-request-name}");

POST

返回网站的$_POST请求值。

post("{post-request-name}");

Cookie

返回网站的$_COOKIE值。

cookie("{cookie-name}");

文件

返回网站的$_FILES值。

files("{file-name}");

会话

返回存储在Web服务器中的网站的$_SESSION值。

session("{session-name}");

请求

请求类的路径包含在includes文件夹中的data_handling.php文件中。

use

use App\DataHandling;

来包含请求类。

$data = new DataHandling();

环境数据

echo $data->env->APP_NAME;

get、post和session请求

echo $data->request->username;
echo $data->request->password;

get请求

echo $data->get->username;
echo $data->get->password;

post请求

echo $data->post->username;
echo $data->post->password;

数据库查询

数据库查询类的路径包含在includes文件夹中的database.php文件中。

use

use App\DB;

来包含数据库类。

记录选择

$data = DB::table("users")->query("where id='1'")->setFetchMode(PDO::FETCH_OBJ)->get();

foreach ($data as $col) {
    echo "Name : " . $col->name . "<br>";
    echo "Age : " . $col->age . "<br>";
    echo "Email : " . $col->email . "<br>";
}

批量记录插入

$data = DB::table("users")->insert([
    ['name' => 'lisara', 'age' => 24, 'email' => 'lisara@gmail.com', 'updated_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s')],
    ['name' => 'sanuli', 'age' => 14, 'email' => 'sanuli@gmail.com', 'updated_at' => date('Y-m-d H:i:s'), 'created_at' => date('Y-m-d H:i:s')],
]);

return print($data); // whether insertion is successfull or not

单条记录插入

$data = DB::table("users")->setTableId("id")->setFetchMode(PDO::FETCH_OBJ)->create(
    [
        'name' => 'lisara',
        'age' => 24,
        'email' => 'lisara@gmail.com'
    ]
);

foreach ($data as $col) {
    echo "Name : " . $col->name . "<br>";
    echo "Age : " . $col->age . "<br>";
    echo "Email : " . $col->email . "<br>";
}

更新记录

$data = DB::table("users")->query("where id='1'")->update(
    [
        'name' => 'Kenura',
        'age' => 19,
        'email' => 'kenura@gmail.com'
    ]
);

return print($data); // count of updated records

删除记录

$data = DB::table("users")->query("where id='16'")->delete();

return print($data); // whether delete is successfull or not

迁移

创建表所需的所有迁移都位于database/migrations文件夹中,每个迁移文件都对应一个表名。

<?php

require_once __DIR__ . '/../../includes/error_handling.php';
require_once __DIR__ . '/../../includes/database.php';
require_once __DIR__ . '/../../includes/data_handling.php';

use App\DB;
use App\DataHandling;

$table_name = "users";

try {
    $users = DB::table($table_name)->migrate([
        "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY",
        "username VARCHAR(256) NOT NULL",
        "password VARCHAR(256) NOT NULL",
    ]);

    if ($users) {
        return print("$table_name migration completed successfully!");
    }
} catch (\Throwable $th) {
    return print("migration error : " . $th->getMessage());
}

要运行如下迁移,请执行以下代码

php databse\migrations\{migration-filename}.php

错误处理

错误处理类的路径包含在includes文件夹中的error_handling.php文件中。

use

use App\ErrorHandling;

来包含ErrorHandling类。

查看404错误

ErrorHandling::_404();

自定义404错误信息

ErrorHandling::_404({error-message});

查看405错误

ErrorHandling::_405();

自定义405错误信息

ErrorHandling::_405({error-message});

查看500错误

ErrorHandling::_500();

自定义500错误信息

ErrorHandling::_500({error-message});