abdursoft php mvc 框架。只需享受工作。它支持多种数据库和新的模板引擎如 blade。现在您可以使用此框架轻松构建项目。它具有默认的 jwt 认证系统

1.0.8 2024-08-08 14:19 UTC

This package is auto-updated.

Last update: 2024-09-08 14:31:09 UTC


README

这是一个由 abdursoft.com 开发的自定义 php mvc 框架。它非常易于使用和定制。现在它支持许多功能,如 Laravel 和 blade 模板。

我们还构建了一个名为 ABS 模板引擎的模板引擎,该引擎集成在此框架中。这将使您的代码非常简化且强大。现在您可以使用 Laravel 中使用的许多功能,例如条件、打印、echo、for 循环、foreach 循环、switch case 等。

您还可以传递页面标题、元标签、自定义样式、javascript 等。此外,您可以使用主布局和子组件,并轻松扩展它们。此模板引擎作为 OOP 架构运行。

在运行项目之前,您需要从 php.ini 文件中启用一些 PHP 扩展。

sodium, tidy, zip, xsl,pdo,mysql,pgsql

现在让我们从 Abdursoft 框架开始。

composer create-project abdursoft/php project_name

安装包后,更新 composer.json 文件内容为 example.composer.json,然后更新 composer

composer update

如果框架在您的设备上成功安装,请转到项目/app 目录。然后从 app\Karnel.php 打开 Karnel 文件。

public static function csrf() {
    return [
        'csrf'    => false,
        'encrypt' => true,
    ];
}

public static function layout() {
    return [
        'minify' => true,
        'mime'   => ['php', 'html', 'htm', 'abs'],
    ];
}

现在更改 CSRF 模式为 true/false。如果您将其设置为 true,则 CSRF 服务将在项目中启动,否则它将被禁用。

之后,您需要更改您想要的 MIME 类型,但请记住,为了其他文件扩展名的连续性。如果您想在页面源视图中对文本进行压缩,则将压缩设置为 true,否则为 false。

现在您必须在 core\Config\Config.php 上设置一些必需的变量。

现在使用您首选的数据更改这些变量。

define( "BASE_URL", 'http://domain.example/' ); //set root directory/domain
define( "SITE_TITLE", 'ABS MVC FRAMEWORK' ); //site name or title
define( "FAV_ICON", BASE_URL . "assets/images/premium.png" ); //site logo
define( 'DEFAULT_KEYWORDS', 'abs mvc developed by abdursoft' ); //Default keywords

define( 'DATABASE_SERVER', 'mysql' ); //supported database mysql,pgsql,mongodb

此外,还有一些变量用于一些特殊功能和包,当您准备使用该包或功能时,请更新它们。

让我们在 route/web.phproute/api.php 中的 web/api 文件上创建一些路由。

Route::get( '', [App::class, 'index'] );

Route::get( '/country', function ( ) {
    $request = new Request();
    echo $request->input('name');
}, ['name'] );

Route::get( '/layout', [App::class, 'layout'] );

Route::group( 'user', function () {
    Route::get( '/profile', [User::class, 'profile'] );
} );

Route::prefix( 'auth' )->group( 'zeroUser', function () {
    Route::post( '/login', [User::class, 'login'] );
} );

Route::withMiddleware( [Authentication::class,'checkAuth'], function () {
    Route::get( '/auth', [App::class, 'auth'] );
} );

现在需要创建/更新 app\Controller\ 中的控制器类。

App.php

namespace ABS\Framework\App\Controller;

use ABS\Framework\Core\Files\Files;
use ABS\Framework\System\Processor\Controller;
use ABS\Framework\System\Request\Request;

use function ABS\Framework\System\Helper\view;

class App extends Controller {
    public function __construct() {
        parent::__construct();
    }
    public function index() {
        $this->load->page_title = "Input validation";
        $this->load->view( 'form');
    }

    public function view() {
        return->view( 'form');
    }

    public function layout() {
        $this->metaContent( 'Hello layout', 'Kmn acho tumi' );
        $this->loadStyle('/css/style.css');
        $this->load->view( 'form' );
    }

    public function auth() {
        echo "Welcome to the auth page";
    }
}

现在需要创建/更新 app\Controller\ 中的控制器类。

User.php

namespace ABS\Framework\App\Controller;

use ABS\Framework\System\Auth\Auth;
use ABS\Framework\System\Auth\Session;
use ABS\Framework\System\Processor\Controller;
use Exception;

use function ABS\Framework\System\Helper\response;

class User extends Controller {
    public function __construct() {
        parent::__construct();
    }
    public function profile( ) {
        try {
            $user = Auth::jwtDecode( $this->request->input['token'] );
            $this->response( [
                'message' => 'server is ok',
                'data'    => $user,
            ], 200 );
        } catch ( Exception $e ) {
            $this->response( [
                'status'  => 0,
                'message' => $e->getMessage(),
            ], 200 );
        }
    }

    public function login( $param ) {
        if ( !empty( $param ) ) {
            $token = Auth::jwtAUTH( $param, 'users' );
            Session::set( 'jwt_token', $token );
            return response( [
                'message'    => 'Login successful',
                'token'      => $token,
                'token_type' => 'Bearer',
            ], 200 );
        }
    }
}

让我们在 app\Middleware 目录中创建/更新 Authentication.php 中间件文件。

namespace ABS\Framework\App\Middleware;

class Authentication {

    public function checkAuth($callback){
        if('hello' == 'hello'){  // your staff
            call_user_func($callback);
        }
        return;
    }

    // Route::get( '/auth', [App::class, 'auth'] ); this route will protect with the checkAuth middleware function. So you can add your condition and return the callback.
}

现在让我们第一次运行项目。

php -S localhost:9000

Web 和 API 的路由系统

web routes
https://domain.example/path_uri
api routes
https://domain.example/api/path_uri

如果项目运行成功,则可以测试所有路由。现在时间更新布局页面和子组件。-首先在 public/view/ 中创建一个 components 文件夹/目录,然后创建一些文件,例如 header.phpfooter.php 以及 layout.php

header.php
<h2>header component</h2>
footer.php
<h2>Footer Component</h2>
layout.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@title@</title>
    @style@
    @script@
    @meta@
</head>
<body>
    @addView('components/header')
    @import(content)
    <img src="{{assets('171924459195spa02ei31jbss784seum66io892spi.png')}}" alt="" width="400px" height="600px">
    @import(body)
    @addView('components/footer')
</body>
</html>

现在让我们在 public/view 中创建一个子视图/组件。

form.php

@extends('components/layout')
@title(layout page)
@export(body)
<h3>Hello text4</h3>
<div>
    <h4>Lorem Text</h4>
    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dignissimos explicabo, quod quibusdam qui accusantium laborum incidunt unde officiis veniam sunt placeat? Nulla illo, molestiae nam ratione numquam quod pariatur quo optio nobis porro unde eius natus quis earum quam in.</p>
</div>

<form action="/action" method="post" enctype="multipart/form-data">
    @csrf
    <input type="text" name="name" value="">
    <input type="text" name="phone">
    <input type="file" name="file" id="">
    <input type="submit" value="submit">
</form>

@endExport

@export(content)
<h3>Content Body</h3>
@endExport

@title@ 将从控制器继承页面标题 @meta@ 将从控制器继承元内容 @style@ 将从控制器继承样式 single|array @script@ 将从控制器继承脚本 @addView(page) 将包含 public/view 目录中的 page.php 文件 @import(content) 将接收来自子组件的 @export(content) @extend(layout) 将继承主 layout 组件和数据 @export(content) 将从子组件导出 @export(content) 数据到布局组件 @Session($item) 将打印 $_SESSION[$item] 的会话值 @Text($nice) 将打印 $nice 的多语言值 @echo($nice) 将打印 $nice 变量 @printf($nice) 将打印 $nice 变量 @print_r($nice) 将打印 $nice 变量 {{$title}} 将打印 $title 的值 {{assets('file_path/file_name)}} 将包含来自 assets 目录的资产文件 {{storage('file_path/file_name)}} 将包含来自 storage/uploads 目录的存储文件 {{resource('file_path/file_name)}} 将包含来自 public/resource 目录的资源文件 {/ php_code /} 执行不带 <?php ?> 开始和结束标记的所有 PHP 代码

其他模板指令

@if('me' === 'me')
    <h2>Equal</h2>
    @elseif(3==3)
       <h2>Else Done</h2>
    @else
        <h2>Not Equal</h2>
@endif

@for($i=0; $i < 10; $i++)
    <h2>{{ $i }}</h2>
@endfor


@foreach($data as $key)
    <h2>{{$key}}</h2>
@endforeach


@switch('hello')
    @case(3)
        @echo('nice match')
        @break
    @case(hello)
        @echo('Hello World')
        @break
    @default
        @echo('nothing')
@endswitch

<h3>数据库和模型</h3> 要创建模型文件,请打开 app/Model 目录,然后根据表名创建一个新的 PHP 文件。如果您数据库中有名为 users 的表,您必须创建名为 Users.php 的模型,其内部内容应如下所示:

namespace ABS\Framework\App\Model;

use ABS\Framework\DB\Model;

class Users extends Model{
    protected static $table;
}

您还可以使用 protected static $table='name_of_the_table' 来更改表名

<h4>模型有什么功能</h4>

-数据插入 -数据读取 -数据更新 -数据删除 -数据聚合 -数据连接

<h4>如何使用模型插入数据</h4>

Route::post( '/user-create', function ( ) {
    $request = new Request();
    Users::create([
        'name' => $request->input('name'),
        'email' => $request->input('email'),
        'phone' => $request->input('phone'),
        'gender' => $request->input('gender')
    ]);
    echo 'User successfully created';
}, ['name'] );

[注意] 用户模型 use alias 是添加到您的控制/路由页面。

<h4>如何使用模型更新数据</h4>

Route::post( '/user-update', function ( ) {
    $request = new Request();
    Users::where('id','=',1)->update([
        'name' => $request->input('name'),
        'email' => $request->input('email'),
        'phone' => $request->input('phone'),
        'gender' => $request->input('gender')
    ]);
    echo 'User successfully updated';
}, ['name'] );

<h4>如何从模型获取单个用户的全部数据</h4>

Route::post( '/user-get-all-single', function ( ) {
    $request = new Request();
    Users::where($column,$operator,$value)->last();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>如何从模型获取单个用户的指定数据</h4>

Route::post( '/user-get-specified', function ( ) {
    $request = new Request();
    Users::select(['name','phone'])->where($column,$operator,$value)->last();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>如何从模型获取全部数据</h4>

Route::post( '/user-get-all', function ( ) {
    $request = new Request();
    Users::where($column,$operator, $value)->get();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>如何从模型按orderby和limit获取全部数据</h4>

Route::post( '/user-get-oder-all', function ( ) {
    $request = new Request();
    Users::orderBy('id', 'DESC')->where($column,$operator,$value)->limit(3,10)->get();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>如何从模型获取全部数据与连接</h4>

Route::post( '/user-get-oder-all', function ( ) {
    $request = new Request();
    $all_items = Items::where('item_id','=',42)->leftJoin('item_category','item_category.id','=','items.category_id')->get();
    echo 'Items successfully retrieved';
}, ['name'] );