PHP code example of samphp / framework

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

    

samphp / framework example snippets


// Application
define('APP_NAME', 'My Application');
define('BASE_URL', 'http://localhost/your-project-name/public');

// Database
define('DB_HOST', 'localhost');
define('DB_NAME', 'your_database');
define('DB_USER', 'root');
define('DB_PASS', '');

// Timezone
date_default_timezone_set('UTC');

// Error Reporting — Set to 0 in production!
error_reporting(E_ALL);
ini_set('display_errors', 1);

// app/controllers/ProductController.php


class ProductController extends Controller
{
    public function index()
    {
        $productModel = $this->model('Product');
        $products = $productModel->getAll();

        $this->view('product/index', ['products' => $products]);
    }

    public function create()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            Security::verifyCsrf($_POST['csrf_token']);

            $name = Security::sanitize($_POST['name']);
            // ... save to database
        }

        $this->view('product/create');
    }
}

// app/models/Product.php


class Product extends Model
{
    public function getAll()
    {
        $stmt = $this->db->query("SELECT * FROM products ORDER BY id DESC");
        return $stmt->fetchAll();
    }

    public function create($data)
    {
        $stmt = $this->db->prepare("INSERT INTO products (name, price) VALUES (?, ?)");
        return $stmt->execute([$data['name'], $data['price']]);
    }
}

<!-- app/views/product/index.php -->
 

// In any controller method
class DashboardController extends Controller
{
    public function index()
    {
        AuthMiddleware::handle();  // Requires login
        // RoleMiddleware::handle(['admin', 'editor']);  // Role-based access

        $this->view('dashboard/index');
    }
}

// In a controller — set a flash message
Session::flash('success', 'Product created successfully!');
$this->redirect('/product');

// In a view — display it
 if ($msg = Session::getFlash('success')):