PHP code example of ace-core / framework

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

    

ace-core / framework example snippets


// Static GET route
$router->get('/about', function() {
    return "This is a custom about page!";
});

// GET Route pointing to a Controller Action
$router->get('/profile', [AuthController::class, 'profile']);

// Parameterized Dynamic Route
$router->get('/users/{id}', function($request, $id) {
    return "User Profile details for ID: " . htmlspecialchars($id);
});

namespace App\Controllers;

use Ace\Controller;
use Ace\Middlewares\AuthMiddleware;

class UserDashboardController extends Controller 
{
    public function __construct() 
    {
        // Require authentication for 'index' and 'editSettings' actions
        $this->registerMiddleware(new AuthMiddleware(['index', 'editSettings']));
    }
    
    public function index($request) {
        return $this->render('dashboard');
    }
}

namespace App\Controllers;

use Ace\Controller;
use Ace\Request;
use Ace\Database;
use App\Services\NotificationService;
use Ace\Middlewares\AuthMiddleware;

class OrderController extends Controller
{
    public function __construct(
        protected Database $db,
        protected NotificationService $notifications
    ) {
        $this->registerMiddleware(new AuthMiddleware());
    }

    public function show(Request $request, $id)
    {
        // $this->db and $this->notifications are already injected!
        return $this->render('orders/show', ['order' => Order::findOrFail($id)]);
    }
}

// Fetch published posts ordered by date limit 10
$posts = Post::query()
    ->where('status', 'published')
    ->orderBy('created_at', 'DESC')
    ->limit(10)
    ->get();

// Find single user or throw ModelNotFoundException (generates 404 response automatically)
$user = User::findOrFail(2);
$user = User::query()->where('email', '[email protected]')->firstOrFail();

// Check existence & pluck specific columns
$exists = User::exists(['email' => '[email protected]']);
$emails = User::pluck('email');

// Option A: Active Record instantiation & save
$user = new User();
$user->name = 'Jane Doe';
$user->email = '[email protected]';
$user->password = 'securePass123';
$user->passwordConfirm = 'securePass123'; // Used in validations

if ($user->validate()) {
    $user->save(); // Inserts and populates $user->id
    echo "Saved user: " . $user->id;
} else {
    print_r($user->errors);
}

// Option B: Inline creation (instantiates, populates, and saves)
$user = User::create([
    'name' => 'Jane Doe',
    'email' => '[email protected]'
]);

// Option C: Update-or-Create
$user = User::updateOrCreate(
    ['email' => '[email protected]'],
    ['name' => 'Jane Doe Jr.']
);

// Delete individual row instance
$user = User::findOne(['id' => 2]);
if ($user) {
    $user->delete();
}

// Statically delete multiple records by primary key
User::destroy(3, 4, 5);

namespace App\Resources;

use Ace\JsonResource;
use Ace\Request;

class UserResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => (int) $this->resource->id,
            'name' => $this->resource->name,
            'email' => $this->resource->email,
            'joined' => date('Y-m-d', strtotime($this->resource->created_at))
        ];
    }
}

// Single resource
$router->get('/api/users/{id}', function(Request $request, $id) {
    return new UserResource(User::findOrFail($id));
});

// Collection of resources
$router->get('/api/users', function(Request $request) {
    return UserResource::collectionResponse(User::all());
});
bash
composer dump-autoload