PHP code example of fran-f / yolk

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

    

fran-f / yolk example snippets



 = new Yolk\App('https://example.com/');

$app->routes()
    ->get('/', HomeController::class)
    ->get('/admin', AdminController::class)
    ->get('/login', LoginController::class, 'login_page')
    ->post('/login', AuthenticateUser::class);

$app->routes()
    ->error(404, NotFoundController::class);

$app->middleware()
    ->only('/admin|/admin/.*', RequireLogin::class);

$app->renderViewsWith(
  fn($view, $data) => (new Twig\Engine)->render($view, $data))
);

$app->run()->output;


use Yolk\Request;

class HomeController
{
    public function __invoke(Request $request)
    {
        return [
            'user' => Session::getLoggedInUser(),
            'articles' => Blog::getRecent(),
        ];
    }
}

$email = $request['email'];

list($name, $surname) = $request->= $request->optional('phone');

# returns an array
$address = $request->optional('address1', 'address2', 'address3');

# returns an associative array
$address = $request->optional([ 'address1', 'address2', 'address3' ]);

  return 404;
  

  # absolute URLs are used as-is
  return 'https://example.com';

  # relative URLs will be prepended with the base URL
  return '/admin'; # → https://example.com/admin

  # route names starting with a ':' will be converted
  return ':login_page'; # → https://example.com/login
  

  return [
    'user' => $user,
    'balance' => $balance,
  ];
  

$app->middleware()
    ->all(ValidateCsrfToken::class)
    ->except('/login', RequireAuthentication::class)
    ->only('/api/.*', EnforceRateLimit::class);

$app = new Yolk\App('https://example.com/');

$di = new SomeLibrary\DependencyInjection()
$app->injectWith(fn($class) => $di->inject($class));

$app->run()->output;