PHP code example of alsocoder / apnaphp

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

    

alsocoder / apnaphp example snippets




use ApnaPHP\Routing\Request;
use ApnaPHP\Routing\Response;

class UsersHandler
{
    public function GET(Request $request)
    {
        return Response::json(['users' => User::all()]);
    }
    
    public function POST(Request $request)
    {
        $user = User::create($request->all());
        return Response::json($user, 201);
    }
}


$users = User::all();



namespace App\Models;

use ApnaPHP\Database\Model;

class User extends Model
{
    protected $table = 'users';
    protected $autoMigrate = true;
    
    protected $schema = [
        'name' => 'ing|in:active,inactive|default:active'
    ];
    
    protected $fillable = ['name', 'email', 'phone', 'age'];
    protected $hidden = ['password'];
}

// Create
$user = User::create([
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

// Find
$user = User::find(1);
$user = User::where('email', '[email protected]')->first();

// Update
$user->name = 'Jane Doe';
$user->save();

// Delete
$user->delete();

// Query Builder
$users = User::where('status', 'active')
    ->where('age', '>', 18)
    ->orderBy('name')
    ->limit(10)
    ->get();



return [
    'name' => env('APP_NAME', 'ApnaPHP Application'),
    'env' => env('APP_ENV', 'production'),
    'debug' => env('APP_DEBUG', false),
    'url' => env('APP_URL', 'http://localhost'),
    'timezone' => env('APP_TIMEZONE', 'UTC'),
];



return [
    'default' => env('DB_DRIVER', 'mysql'),
    
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', 3306),
            'database' => env('DB_DATABASE', ''),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
        ],
        // ... other connections
    ],
];
bash
php apna serve

my-app/
├── app/
│   ├── api/                 # API routes
│   │   └── users/
│   │       └── route.apna.php
│   ├── dashboard/           # Page routes
│   │   └── page.apna.php
│   └── layout.apna.php      # Layout files
├── config/
│   ├── app.php
│   └── database.php
├── models/
│   └── User.php
├── public/
│   └── index.php
└── storage/
    ├── cache/
    ├── logs/
    └── uploads/