PHP code example of simplemvc / framework

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

    

simplemvc / framework example snippets


chdir(dirname(__DIR__));
;
use SimpleMVC\App;
use SimpleMVC\Emitter\SapiEmitter;

$builder = new ContainerBuilder();
$builder->addDefinitions('config/container.php');
$container = $builder->build();

$app = new App($container);
$app->bootstrap(); // optional
$response = $app->dispatch(); // PSR-7 response

SapiEmitter::emit($response);

// config/container.php
use App\Controller;

return [
    'config' => [
        'routing' => [
            'routes' => [
                [ 'GET', '/', Controller\HomePage::class ]
            ]
        ]
    ]
];

// config/container.php
use App\Controller;
use SimpleMVC\Controller\BasicAuth;

return [
    'config' => [
        'routing' => [
            'routes' => [
                [ 'GET', '/admin', [BasicAuth::class, Controller\HomePage::class ]
            ]
        ],
        'authentication' => [
            'username' => 'admin',
            'password' => '1234567890'
        ]
    ]
];

// config/container.php
use App\Controller;

return [
    'config' => [
        'routing' => [
            'routes' => [
                [ 'GET', '/', [Controller\A::class, Controller\B::class ]
            ]
        ]
    ]
];

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SimpleMVC\Controller\AttributeInterface;
use SimpleMVC\Controller\AttributeTrait;
use SimpleMVC\Controller\ControllerInterface;

class A implements ControllerInterface, AttributeInterface
{
    use AttributeTrait;

    public function execute(
        ServerRequestInterface $request, 
        ResponseInterface $response
    ): ResponseInterface
    {
        $this->addRequestAttribute('foo', 'bar');
        return $response;
    }
}

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SimpleMVC\Controller\ControllerInterface;

class B implements ControllerInterface
{
    public function execute(
        ServerRequestInterface $request, 
        ResponseInterface $response
    ): ResponseInterface
    {
        $attribute = $request->getAttribute('foo');
        pritnf("Attribute is: %s", $attribute);
        return $response;
    }
}