PHP code example of phastasf / framework

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

    

phastasf / framework example snippets




e('BASE_PATH', __DIR__.'/..');

$framework = new Phast\Framework;

$framework->getWebEntrypoint()->handle();

#!/usr/bin/env php

IR__);

$framework = new Phast\Framework;

exit($framework->getConsoleEntrypoint()->run());

// config/app.php
return [
    'debug' => env('APP_DEBUG', false),
    'controllers' => ['namespace' => 'App\\Controllers'],
    'models' => ['namespace' => 'App\\Models'],
    'jobs' => ['namespace' => 'App\\Jobs'],
];

// config/database.php
return [
    'driver' => env('DB_DRIVER', 'mysql'),
    'migrations' => BASE_PATH.'/database/migrations',
    'mysql' => [
        'host' => env('DB_HOST', 'localhost'),
        'database' => env('DB_DATABASE', ''),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
    ],
];

// config/session.php
return [
    'cookie' => [
        'name' => env('SESSION_COOKIE', 'PHPSESSID'),
        'lifetime' => (int) env('SESSION_LIFETIME', 7200),
        'path' => env('SESSION_PATH', '/'),
        'domain' => env('SESSION_DOMAIN', null),
        'secure' => env('SESSION_SECURE', false),
        'httponly' => env('SESSION_HTTPONLY', true),
        'samesite' => env('SESSION_SAMESITE', 'Lax'), // 'Strict', 'Lax', or 'None'
    ],
];

// config/proxies.php
return [
    'trusted' => [
        '10.0.0.0/8',      // Private network
        '172.16.0.0/12',  // Docker network
        '192.168.0.0/16', // Private network
        '127.0.0.1',      // Localhost IPv4
        '::1',            // Localhost IPv6
        // Add your production proxy IPs here
        // '203.0.113.0/24', // Your load balancer IP range
    ],
    'headers' => [
        'Forwarded',
        'X-Forwarded-For',
        'X-Real-Ip',
        'Client-Ip',
    ],
];

// routes/web.php
return function (Router $router) {
    $router->get('/', 'HomeController@index');
    $router->get('/users/{id}', 'UserController@show');
    $router->post('/users', 'UserController@store');
};

// config/middleware.php
return [
    // Core framework middleware (dleware\SessionMiddleware::class,
    // CORS middleware (should be early in pipeline to handle preflight requests)
    \Phast\Middleware\CorsMiddleware::class,
    // Client IP detection middleware (must be before routing if behind proxy)
    // \Phast\Middleware\ClientIpMiddleware::class,
    // Add AuthMiddleware here if you want authentication
    // \Phast\Middleware\AuthMiddleware::class,
    // Add your custom middleware here (before routing)
    \App\Middleware\CustomMiddleware::class,
    \Phast\Middleware\RoutingMiddleware::class,
    \Phast\Middleware\DispatcherMiddleware::class,
];

// config/cors.php
return [
    // Allowed origins ('*' for all, or array of specific origins)
    // Note: Cannot use '*' when allow_credentials is true
    'allowed_origins' => env('CORS_ALLOWED_ORIGINS', '*'),

    // Allowed HTTP methods
    'allowed_methods' => [
        'GET',
        'POST',
        'PUT',
        'PATCH',
        'DELETE',
        'OPTIONS',
    ],

    // Allowed request headers ('*' for all, or array of specific headers)
    'allowed_headers' => env('CORS_ALLOWED_HEADERS', '*'),

    // Headers that can be exposed to the client
    'exposed_headers' => [],

    // Maximum age (in seconds) for preflight request cache
    'max_age' => (int) env('CORS_MAX_AGE', 86400),

    // Allow credentials (cookies, authorization headers, etc.)
    // Note: When true, allowed_origins cannot be '*'
    'allow_credentials' => (bool) env('CORS_ALLOW_CREDENTIALS', false),

    // Paths/prefixes to 

'allowed_origins' => '*',
'allow_credentials' => false,

'allowed_origins' => [
    'https://example.com',
    'https://www.example.com',
],
'allow_credentials' => true,

''exclude' => [],

''exclude' => ['/admin', '/internal'],

// config/providers.php
return [
    // ConfigProvider must be first (other providers depend on it)
    \Phast\Providers\ConfigProvider::class,
    \Phast\Providers\CacheProvider::class,
    \Phast\Providers\DatabaseProvider::class,
    // ... other framework providers
    // Add your custom providers here
    \App\Providers\CustomProvider::class,
];

namespace App\Providers;

use Katora\Container;
use Phast\Providers\ProviderInterface;

class CustomProvider implements ProviderInterface
{
    public function provide(Container $container): void
    {
        // Register services here
        $container->set('custom.service', fn() => new CustomService);
    }

    public function init(Container $container): void
    {
        // Initialize services here (called after all providers are registered)
        $service = $container->get('custom.service');
        $service->initialize();
    }
}

namespace App\Controllers;

use Phast\Controller;

class HomeController extends Controller
{
    public function index(ServerRequestInterface $request): ResponseInterface
    {
        return $this->render('welcome', ['message' => 'Hello, Phast!']);
    }
}

namespace App\Models;

use Datum\Model;

class User extends Model
{
    protected static ?string $table = 'users';
}

// Usage
$user = User::find(1);
$user = new User;
$user->name = 'John';
$user->save();

// Generated migration
class CreateUsersTable implements MigrationInterface
{
    public function up(ConnectionInterface $connection): void
    {
        $connection->execute("CREATE TABLE users (...)");
    }

    public function down(ConnectionInterface $connection): void
    {
        $connection->execute("DROP TABLE users");
    }
}

// Generated seeder (implements Phast\Database\SeederInterface)
// Dependencies are injected via constructor and resolved by the container.
use Databoss\ConnectionInterface;
use Phast\Database\SeederInterface;

class UserSeeder implements SeederInterface
{
    public function __construct(
        private readonly ConnectionInterface $connection
    ) {}

    public function run(): void
    {
        // Raw connection
        $this->connection->insert('users', ['name' => 'John', 'email' => '[email protected]']);

        // Or via model
        $user = new \App\Models\User;
        $user->name = 'Jane';
        $user->email = '[email protected]';
        $user->save();
    }
}

'seed' => [
    'DatabaseSeeder',
    'UserSeeder',
],

namespace App\Jobs;

use Qatar\Job;

class SendEmailJob extends Job
{
    public function handle(array $payload): void
    {
        // Job logic
    }

    public function retries(): int
    {
        return 3;
    }
}

$cache = $container->get(CacheInterface::class);
$cache->set('key', 'value', 3600);
$value = $cache->get('key');

$logger = $container->get(LoggerInterface::class);
$logger->info('User logged in', ['user_id' => 123]);

use Psr\Http\Client\ClientInterface;

$client = $container->get(ClientInterface::class);
$request = $requestFactory->createRequest('GET', 'https://api.example.com/data');
$response = $client->sendRequest($request);

use Filtr\Validator;

$validator = new Validator;
$validator->r->validate($data);

// config/auth.php
return [
    'jwt' => [
        'secret' => env('JWT_SECRET'),
        'algorithm' => 'HS256',
    ],
    'middleware' => [
        'include' => ['/api/*'],
        'exclude' => ['/api/auth/*'],
        '