nicetwo/nicetwo-framework

NiceTwo 框架是一个为生产准备的开源 PHP 框架。

安装: 17

依赖: 0

建议: 0

安全: 0

星标: 4

关注者: 0

分支: 1

开放问题: 0

类型:项目

1.0.0 2022-05-07 02:53 UTC

This package is auto-updated.

Last update: 2024-09-29 05:50:08 UTC


README

NiceTwo 框架是一个为生产准备的开源 PHP 框架

安装

使用 Composer 安装 NiceTwo 框架。

composer create-project nicehalf/nicehalf-framework

应用程序使用

# Get composer autoload
require __DIR__ . '/../vendor/autoload.php';

# Get application class
require __DIR__ . '/../system/Applications/Application.php';

# Run the app
Application::run();

Cookie 使用

# Get cookie class
require __DIR__ . '/../system/Cookies/Cookie.php';

# Or use import cookie class
use NicehalfCore\System\Cookies\Cookie;

# Set cookie , you can set expire time by edit the third parameter 
Cookie::set('name', 'value');

# Check that cookie has the key
if (Cookie::has('name')) {
    echo 'has cookie';
}

# Get cookie by the given key
echo Cookie::get('name');

# Delete cookie by the given key
Cookie::delete('name');

# Return all cookies
print_r(Cookie::all());

# Destroy all cookies
Cookie::destroy();

Session 使用

# Get session class
require __DIR__ . '/../system/Sessions/Session.php';

# Or use import session class
use NicehalfCore\System\Sessions\Session;

# Start session
Session::start();

# Set session
Session::set('name', 'value');

# Check that session has the key
if (Session::has('name')) {
    echo 'has session';
}

# Get session by the given key
echo Session::get('name');

# Delete session by the given key
Session::delete('name');

# Return all sessions
print_r(Session::all());

# Get flash session by the given key
Session::flash('name');

# Destroy all sessions
Session::destroy();

数据库使用

# Get database class
require __DIR__ . '/../system/Database/Database.php';

# Or use import database class
use NicehalfCore\System\Database\Database;

# Connect to database
# Database connection start only when you call a method and you can use the following methods :

# Select
Database::table('table_name')->select('column_name');

# Insert
Database::table('table_name')->insert(['column_name' => 'value'])->execute();

# Update
Database::table('table_name')->update(['column_name' => 'value'])->where('column_name', '=', 'value');

# Delete
Database::table('table_name')->delete()->where('column_name', '=', 'value');

# To join table
Database::table('table_name')->join('table_name', 'table_name.column_name', '=', 'table_name.column_name');

# To join table on left
Database::table('table_name')->leftJoin('table_name', 'table_name.column_name', '=', 'table_name.column_name');

# To join table on right
Database::table('table_name')->rightJoin('table_name', 'table_name.column_name', '=', 'table_name.column_name');

# Usage of where
Database::table('table_name')->where('column_name', '=', 'value');

# Usage of orWhere 
Database::table('table_name')->orWhere('column_name', '=', 'value');

# Filter string : filtring inputs is automatically handled by the framework

# Usage of groupBy
Database::table('table_name')->groupBy('column_name');

# Usage of orderBy
Database::table('table_name')->orderBy('column_name', 'asc');

# Usage of limit
Database::table('table_name')->limit(10);

# Usage of offset
Database::table('table_name')->offset(10);

# Usage of having
Database::table('table_name')->having('column_name', '=', 'value');

# Usage of get
print_r(Database::table('table_name')->get());

# Get first row
print_r(Database::table('table_name')->first());

# Usage of count
print_r(Database::table('table_name')->count());

# Usage of patination
print_r(Database::table('table_name')->paginate(10));

# Get links for pagination
print_r(Database::table('table_name')->links()); // use it in view

# Usage of clear
Database::table('table_name')->clear(); // automatically handled by the framework

Exception 使用

# Get exception class
require __DIR__ . '/../system/Exeptions/Whoops.php';

# Or use import exception class
use NicehalfCore\System\Exeptions\Whoops;

# Throw exception
throw new Exception('message');

File 使用

# Get file class
require __DIR__ . '/../system/Extra/File.php';

# Or use import file class
use NicehalfCore\System\Extra\File;

# Get Root path
echo File::root();

# Get directory separator
echo File::ds();

# Get file full path
echo File::path('path/to/file');

# Check if file exists
echo File::exist('path/to/file');

# Require file
File::require_file('path/to/file');

# Include file
File::include_file('path/to/file');

# Require directory
File::require_directory('path/to/dir');

消息使用:使用 Bootstrap

# Get message class
require __DIR__ . '/../system/Extra/Messages.php';

## Or use import message class
use NicehalfCore\System\Extra\Messages;

## Set Error Message
Messages::error('message');

## Set Success Message
Messages::success('message');

## Set Info Message
Messages::info('message');

## Set Warning Message
Messages::warning('message');

Url 使用

# Get url class
require __DIR__ . '/../system/Extra/Url.php';

# Or use import url class
use NicehalfCore\System\Extra\Url;

# Get path
echo Url::path($path);

# Get current url
echo Url::getCurrentUrl();

# Go to previous url
Url::previous();

## Redirect to url
Url::redirect('url');

Request 使用

# Get request class
require __DIR__ . '/../system/Http/Request.php';

# Or use import request class
use NicehalfCore\System\Http\Request;

# Get Base Url
echo Request::baseUrl();

# Get full url
echo Request::full_url();

# Check that the request has the key
if (Request::has('name')) {
    echo 'has request';
}

# Get the value from the request by the given key and method
echo Request::value('name', 'post');

# Get value from get request
echo Request::get('name');

# Get value from post request
echo Request::post('name');

# Set value for request by the given key
Request::set('name', 'value');

# Get previous request value
echo Request::previous();

# Get all requests
print_r(Request::all());

Response 使用

# Get response class
require __DIR__ . '/../system/Http/Response.php';

# Or use import response class
use NicehalfCore\System\Http\Response;

# Set response header
Response::header('Content-Type', 'text/html'); 

# Set response status code
Response::status(200);

# Return json respoonse
Response::json(['name' => 'value']);

# Output data : accept string, array, object
Response::output('data');

Server 使用

# Get server class
require __DIR__ . '/../system/Http/Server.php';

# Or use import server class
use NicehalfCore\System\Http\Server;

# Check that server has the key
if (Server::has('name')) {
    echo 'has server';
}

# Get the value from server by the given key
echo Server::value('name');

# Get path info
echo Server::path_info();

# Get all server data
print_r(Server::all());

Model 使用

# Get model class
require __DIR__ . '/../system/Models/Model.php';

# Or use import model class
use NicehalfCore\System\Models\Model;

# you can use the model class to create your own model for your application , for example : create a model for user and on the model you can create your own methods and set the table name without using Database class

# for example without using model :
# Database::table('users')->where('id', '=', 1)->first();

# for example with model :
# Model::where('id', '=', 1)->first();

Route 使用

# Get route class
require __DIR__ . '/../system/Routers/Route.php';

# Or use import route class
use NicehalfCore\System\Routers\Route;

# Set route on get method
Route::get('/', function () {
    return 'Hello World';
});
# or
Route::get('/', "Controller@method");

# Set route on post method
Route::post('/', function () {
    return 'Hello World';
});
# or
Route::post('/', "Controller@method");

# Set route on both methods
Route::any('/', function () {
    return 'Hello World';
});
# or
Route::any('/', "Controller@method");

# Usage of prefix on get or post method
Route::prefix('admin', function () {
    Route::get('dashboard', function () {
        return 'Hello World';
    });
    # or
    Route::get('dashboard', "Controller@method");
});

# Usage of middleware
Route::middleware('auth', function () {
    Route::get('dashboard', function () {
        return 'Hello World';
    });
    # or
    Route::get('dashboard', "Controller@method");
});

# Get All routes
print_r(Route::all());

View 使用

# Get view class
require __DIR__ . '/../system/Views/View.php';

# or use import view class
use NicehalfCore\System\Views\View;

# render view by class
View::render('view', ['name' => 'value']);

# render view by helper
view('view', ['name' => 'value'], 'helper');

# you can use blade template engine or basic method to render view
# blade template engine is the default method
# to change the method you can go to view class and change the method

# example of blade template engine :
public static function render($path, $data = [])
{
    $errors = Session::flash('errors');
    $old = Session::flash('old');
    $data = array_merge($data, ['errors' => $errors, 'old' => $old]);

    return static::bladeRender($path, $data);
}

# example basic method :
public static function render($path, $data = [])
{
    $errors = Session::flash('errors');
    $old = Session::flash('old');
    $data = array_merge($data, ['errors' => $errors, 'old' => $old]);

    return static::viewRender($path, $data);
}

Validator 使用

# Get validator class
require __DIR__ . '/../system/Validations/Validator.php';

# Or use import validator class
use NicehalfCore\System\Validations\Validator;

# Validate data
$validator = Validator::make($data, [
    'name' => 'required|min:3',
    'email' => 'required|email',
    'password' => 'required|min:6',
]);

贡献

欢迎提交拉取请求。对于主要更改,请先提交一个问题来讨论您想进行的更改。

请确保适当地更新测试。

版本

1.0.0

作者

由 Ayoub Bablil 开发。

邮箱

ayoubbablil@gmail.com

网站

https://nicehalf.com

许可证

该框架是开源软件,许可协议为 MIT 许可证。# NiceTwo-Framework