PHP code example of arout / rhapsody-core

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

    

arout / rhapsody-core example snippets



namespace App\Controllers;

use App\Models\User;
use Rhapsody\Core\BaseController;
use Rhapsody\Core\Http\Request;
use Rhapsody\Core\Http\Response;

// Your controller with automatic dependency injection
class UserController extends BaseController
{
    public function __construct(protected User $userModel) {}

    public function show(Request $request, string $user_id): Response
    {
        // Find the user
        $user = $this->userModel->findById($user_id);

        // Tell the controller which view file to render, and pass
        // the user's data to the view
        return $this->view('users/show.twig', ['user' => $user]);
    }
}


// Note that '{user_id}' is an example of how to create dynamic ($_GET) routes.
// We chain the ->middleware('auth') middleware to the route in order to ensure
// only logged in users can view this page (replace 'auth' with 'admin' to do the
// same to restrict pages to only administrators
Router::get('/users/{user_id}', [UserController::class, 'show'])
    ->middleware('auth');