Git Product home page Git Product logo

yolk's Introduction

Yolk - A toy framework for small projects

Yolk is a small router-plus-dispatcher library for PHP applications running under Apache.

  • Routes to controllers
  • Request pre-processors (middleware)
  • Structured access to request parameters
  • Friendly syntax for common responses
  • Path generation for named routes
  • Error pages and exception handling
  • Supports dependency injection

Sample front controller

<?php
require 'vendor/autoload.php';

$app = 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;

Sample route controller

<?php
use Yolk\Request;

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

Request parameters

Controllers are invoked with a single parameter, an instance of Yolk\Request which wraps all request parameters, in order of priority: route elements, query string (GET), and body parameters (POST).

The request object can be accessed as an array. Missing parameters will have null as value.

$email = $request['email'];

The optional() and required() methods support accessing multiple parameters at the same time. If a required() parameter is missing, Yolk will immediately return a 400 response.

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

# returns a single value
$phone = $request->optional('phone');

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

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

The all() method returns the full set of parameters.

Short responses

Controllers can return an instance of Yolk\Response (which supports status codes, headers, etc.) but they can also return shortcut values:

  • integers for status codes

    return 404;

    Yolk will call the error controller registered for that status code. If no controller has been configured, it will render a basic error page.

  • strings for 302 redirects

    # 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
  • arrays to render a default view

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

    Yolk will pass the controller name and the array to the rendering callback, which can derive the name of the view and render its content.

Middleware

Before reaching a controller, Yolk can pass the Request object to a chain of pre-processors. Each can inspect the Request, and decide to leave it as it is and continue the chain (returning null), replace it (returning a new Request), or interrupt the chain (returning a Response).

Pre-processors can be configured for all or a subset of the routes:

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

Dependency injection

By default, Yolk creates instances of controllers without passing any parameters. To rely on a dependency injection library, use the injectWith() method:

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

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

$app->run()->output;

yolk's People

Contributors

fran-f avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.