PHP code example of karan-safaie-qadi / backend-system

1. Go to this page and download the library: Download karan-safaie-qadi/backend-system 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/ */

    

karan-safaie-qadi / backend-system example snippets


'auth' => [
    'registration_mode' => 'email', // 'email' or 'phone'
    'password_min_length' => 8,
],
'access_levels' => [
    1 => 'user',
    2 => 'admin',
    3 => 'owner',
    // 4 => 'editor', // add custom levels
],



use Database\Database;
use App\Core\Config;

Database::setRootPath(__DIR__);
Config::load(__DIR__ . '/config');

use App\Services\AuthService;

// Register (mode is 'email')
$user = AuthService::register([
    'username' => 'johndoe',
    'email' => '[email protected]',
    'password' => 'securePass123',
    'display_name' => 'John Doe',
]);

// Login
$user = AuthService::login('johndoe', 'securePass123');

// Get current user
$user = AuthService::getCurrentUser();

// Logout
AuthService::logout();

use App\Services\ProductService;

// Create product
$product = ProductService::createProduct([
    'name' => 'Smartphone X',
    'price' => 699.99,
    'stock_quantity' => 50,
    'category_id' => 1,
]);

// Get all products (paginated)
$products = ProductService::getAllProducts(page: 1, perPage: 20);

// Search
$results = ProductService::searchProducts('smartphone');

// Update stock
ProductService::updateStock(1, 100);

use App\Services\ArticleService;

// Create article with sections
$article = ArticleService::createArticle([
    'title' => 'Getting Started with PHP',
    'summary' => 'A comprehensive guide',
    'category_id' => 2,
    'author_id' => 1,
], [
    ['title' => 'Introduction', 'section_type' => 'text', 'content' => 'PHP is a popular language...'],
    ['title' => 'Key Features', 'section_type' => 'list', 'list_items' => ['Easy to learn', 'Widely used', 'Great community']],
    ['title' => 'Comparison', 'section_type' => 'table', 'table_data' => [
        ['Feature', 'PHP', 'Python'], ['Speed', 'Fast', 'Moderate'], ['Learning', 'Easy', 'Easy']
    ]],
]);

// Get article with sections and TOC
$article = ArticleService::getArticle(1);
// $article['sections'] - all sections
// $article['toc'] - table of contents

// Publish
ArticleService::publishArticle(1);

use App\Auth\AccessControl;

// Check permissions
if (AccessControl::isAdmin($userLevel)) { /* user is admin+ */ }
if (AccessControl::isOwner($userLevel)) { /* user is owner */ }
if (AccessControl::canManageAdmins($userLevel)) { /* only owner */ }

// Enforce minimum level
AccessControl::

use App\Services\SystemService;

// Register custom methods
SystemService::registerMethod('hello', function($name) {
    return "Hello, $name!";
});

// Call them
echo SystemService::callMethod('hello', 'World'); // Hello, World!

// Utility methods
$slug = SystemService::generateSlug('Hello World'); // 'hello-world'
$clean = SystemService::sanitizeInput('<script>alert("xss")</script>');

'auth' => [
    'registration_mode' => 'email', // 'email' یا 'phone'
    'password_min_length' => 8,
],
'access_levels' => [
    1 => 'user',
    2 => 'admin',
    3 => 'owner',
    // 4 => 'editor', // سطوح دلخواه اضافه کنید
],



use Database\Database;
use App\Core\Config;

Database::setRootPath(__DIR__);
Config::load(__DIR__ . '/config');

use App\Services\AuthService;

// ثبت‌نام کاربر
$user = AuthService::register([
    'username' => 'ali',
    'email' => '[email protected]',
    'password' => '12345678',
]);

// ورود
$user = AuthService::login('ali', '12345678');

// خروج
AuthService::logout();

use App\Services\ProductService;

// ایجاد محصول
$product = ProductService::createProduct([
    'name' => 'گوشی هوشمند X',
    'price' => 25000000,
    'stock_quantity' => 50,
]);

// جستجو
$results = ProductService::searchProducts('گوشی');

use App\Services\ArticleService;

$article = ArticleService::createArticle([
    'title' => 'آموزش PHP',
    'summary' => 'یک راهنمای جامع',
], [
    ['title' => 'مقدمه', 'section_type' => 'text', 'content' => 'PHP یک زبان محبوب است...'],
    ['title' => 'ویژگی‌ها', 'section_type' => 'list', 'list_items' => ['آسان', 'قدرتمند', 'رایگان']],
]);