PHP code example of nicehalf / nicehalf-framework

1. Go to this page and download the library: Download nicehalf/nicehalf-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nicehalf / nicehalf-framework example snippets


# Get composer autoload

n::run();

# Get cookie class
okie 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();

# Get session class
sion 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
base 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

# Get exception class
tion class
use NicehalfCore\System\Exeptions\Whoops;

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

# Get file class
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::

# Get message class
ssage 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');

# Get url class
 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');

# Get request class
uest 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());

# Get response class
onse 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');

# Get server class
rver 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());

# Get model class
odel 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();

# Get route class
oute 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());

# Get view class
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);
}

# Get validator class
ator class
use NicehalfCore\System\Validations\Validator;

# Validate data
$validator = Validator::make($data, [
    'name' => '