PHP code example of sonypradana / php-mvc

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

    

sonypradana / php-mvc example snippets


// database/migration/<timestamp>_profiles.php
Schema::table('profiles', function (Create $column) {
    $column('user')->varChar(32);
    $column('real_name')->varChar(100);
    $column->primaryKey('user');
})

// app/Controller/ProfileController.php
public function index(MyPDO $pdo): Response
{
    return view('profile', [
        'name' => Profile::find('pradana', $pdo)->real_name
    ]);
}

// resources/views/profile.template.php
{% extend('base/base.template.php') %}
{% section('title', 'Welcome {{ $name }}') %}

{% section('content') %}
    <h1>Hello, {{ $name }}! 👋</h1>
{% endsection %}

// route/web.php
Router::get('/profile', [ProfileController::class, 'index']);

// app/Services/ProfileServices.php
#[Get('/api/v1/profile')]
#[Name('api.v1.profile')]
#[Middleware([AuthMiddleware::class])]
public function index(MyPDO $pdo): array
{
    $data = Cache::remember('profile', 3600, fn () => [
        'name'   => Profile::find('pradana', $pdo)->real_name,
        'status' => 200,
    ]);

    return JsonResponse($data);
}

Router::register([
    ProfileServices::class,
    // add more class
]);

Route::get('/api/v1/profile', [ProfileServices::class, 'index'])
    ->name('api.v1.profile')
    ->middleware([AuthMiddleware::class]);
bash
php cli serve
bash
php cli make:migration profiles
php cli db:create  # Only if database doesn't exist yet
bash
php cli migrate
bash
php cli make:controller Profile
bash
php cli make:view profile
bash
php cli make:services Profile