PHP code example of nixphp / framework

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

    

nixphp / framework example snippets


// /bootstrap.php

define('BASE_PATH', __DIR__);

 the application

//File: app/routes.php

router()->add('GET', '/hello', [HelloController::class, 'index']);

//File: app/Controllers/HelloController.php

namespace App\Controllers;

class HelloController
{
    public function index()
    {
        return view('hello', ['name' => 'World']);
    }
}

//File: app/views/hello.phtml

use function NixPHP\s; // Sanitize on output (provided through nixphp/view)

<h1>Hello, <?= s($name) 

use function NixPHP\app;

// Register interfaces (n() => new FileLogger());
app()->container()->set(DatabaseInterface::class, fn() => new MySQLDatabase());

class UserService {
    public function __construct(
        private LoggerInterface $logger  // ✅ Automatically injected
    ) {}
}

class UserController {
    public function __construct(
        private UserService $service,    // ✅ Auto-built and injected
        private LoggerInterface $logger  // ✅ Retrieved from container
    ) {}
}

// No manual wiring needed - the dispatcher handles it automatically!
router()->add('GET', '/users', [UserController::class, 'index']);

// Build a fresh instance
$command = app()->container()->make(MigrateCommand::class);

// Build with custom parameters
$handler = app()->container()->make(RequestHandler::class, [
    'timeout' => 30,
    'retries' => 3
]);

// Build as a singleton (stored in the container for reuse)
$service = app()->container()->make(CacheService::class, singleton: true);

/app
    /Controllers
    /Models
    /Services
    config.php
    routes.php
/public
    index.php
bootstrap.php
composer.json

my-plugin/
├── src/
│   └── config.php
├── views/
│   └── errors/404.phtml
├── bootstrap.php
└── composer.json