PHP code example of danc0 / gimliduck-php

1. Go to this page and download the library: Download danc0/gimliduck-php 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/ */

    

danc0 / gimliduck-php example snippets


declare(strict_types=1);
use Gimli\Router\Route;

Application::create(__DIR__, $_SERVER);

Route::get('/', function(){
	echo "Hello World";
});

Application::start();


declare(strict_types=1);
p\Core\Config;
use App\Core\Cache;

define('APP_ROOT', __DIR__);

$App = Application::create(APP_ROOT, $_SERVER);

// set up your config and add it to the Application
$config_file = parse_ini_file(APP_ROOT . '/App/Core/config.ini', true);
$App->Config = $App->Injector->resolveFresh(Config::class, ['config' => $config_file]);

// Register a cache class with the Injector
$App->Injector->register(Cache::class, Cache::getCache($App->Config->admin_cache));

// Run Application
$App->run();

// Load routes from a file(s)
$App->loadRouteFiles([
	'App/routes/web.php',
]);


Route::get('/', function(){
	echo "Hello World"
});

// Single action controller, must use __invoke method
Route::get('/', Home_Controller::class);
// cli routes are single action Job classes
Route::cli('build-cache', Cache_Job::class);

Route::get('/', Home_Controller::class . '@homePage');

Route::get('/', [Home_Controller::class, 'homePage']);

Route::get('/', [Home_Controller::class, 'homePage'])->addMiddleware(Logged_In_Middleware::class);

protected array $patterns = [
	':all' => "([^/]+)",
	':alpha' => "([A-Za-z_-]+)",
	':alphanumeric' => "([\w-]+)",
	':integer' => "([0-9_-]+)",
	':numeric' => "([0-9_-.]+)",
	':id' => "([0-9_-]+)",
	':slug' => "([A-Za-z0-9_-]+)",
];

Route::get('/user/:integer#id', [User_Controller::class, 'getUser']);

public function getUser(Response $Response, int $id): Response {
	// do something with $id
}


declare(strict_types=1);
namespace App\Controllers;

use App\Logic\Dashboard_Logic;
use Gimli\Http\Response;
use Gimli\Application;
use Gimli\View\Latte_Engine;

class Dashboard_Landing_Controller {

	/**
	 * Constructor
	 *
	 * @param Application $Application
	 */
	public function __construct(
		public Application $Application,
		protected Dashboard_Logic $Dashboard_Logic,
		protected Latte_Engine $View
	){
		//
	}

	/**
	 * Single action controller call
	 *
	 * @return Response
	 */
	public function __invoke(Response $Response): Response {		
		$template_data = $this->Dashboard_Logic->getTemplateData();
		return $Response->setResponse($this->View->render('dashboard/dashboard.latte', $template_data));
	}
}


declare(strict_types=1);

namespace Gimli\Database;

use Gimli\Database\Model;
use Gimli\Database\Seed;


class User_Model extends Model {
	
	/**
	 * @var string $table_name
	 */
	protected string $table_name = 'users';

	
	/**
	 * ID
	 * 
	 * @var int $id 
	 */
	public $id;

	/**
	 * Unique_Id
	 * 
	 * @var string $unique_id 
	 */
	#[Seed(type: 'unique_id', args: ['length' => 12])]
	public $unique_id;

	/**
	 * Username
	 * 
	 * @var string $username 
	 */
	#[Seed(type: 'username')]
	public $username;

	/**
	 * Email
	 * 
	 * @var string $email 
	 */
	#[Seed(type: 'email')]
	public $email;

	/**
	 * Password
	 * 
	 * @var string $password 
	 */
	#[Seed(type: 'password')]
	public $password;

	/**
	 * Is_Active
	 * 
	 * @var int $is_active 
	 */
	#[Seed(type: 'tiny_int')]
	public $is_active;

	/**
	 * First Name
	 * 
	 * @var string $first_name 
	 */
	#[Seed(type: 'first_name')]
	public $first_name;

	/**
	 * Last Name
	 * 
	 * @var string $last_name 
	 */
	#[Seed(type: 'last_name')]
	public $last_name;

	/**
	 * Status
	 * 
	 * @var int $status 
	 */
	#[Seed(type: 'one_of', args: [0,1])]
	public $status;

	/**
	 * Created_At
	 * 
	 * @var string $created_at 
	 */
	#[Seed(type: 'date', args: ['format' => 'Y-m-d H:i:s', 'min' => '2021-01-01', 'max' => '2021-04-01 00:00:00'])]
	public $created_at;

	/**
	 * Updated_At
	 * 
	 * @var string $updated_at 
	 */
	#[Seed(type: 'date', args: ['format' => 'Y-m-d H:i:s', 'min' => '2021-04-01 00:02:00'])]
	public $updated_at;

	/**
	 * bio
	 * 
	 * @var string $about 
	 */
	#[Seed(type: 'paragraph', args: ['count' => 1])]
	public $about;
}

Seeder::make(User_Model::class)
	->seed(123)
	->count(1)
	->create();

Seeder::make(User_Model::class)
	->seed(123)
	->count(1)
	->callback(function($data) {
		return [
			Seeder::make(User_Hobby_Model::class)->with(['user_id' => $data['id']]),
			Seeder::make(User_Group_Model::class)->with(['user_id' => $data['id']]),
		]
	})
	->create();