GithubHelp home page GithubHelp logo

dispatch's Introduction

dispatch

  • a tiny library for quick and easy PHP apps
  • requires PHP 5.6+

Here's a sample of how you'd usually use dispatch.

<?php

require 'path/to/dispatch.php';

# sample JSON end point
route('GET', '/books.json', function ($db, $config) {
  $list = loadAllBooks($db);
  $json = json_encode($list);
  return response($json, 200, ['content-type' => 'application/json']);
});

# html end point
route('GET', '/books/:id', function ($args, $db) {
  $book = loadBookById($db, $args['id']);
  $html = phtml(__DIR__.'/views/book', ['book' => $book]);
  return response($html);
});

# respond using a template
route('GET', '/about', page(__DIR__.'/views/about'));

# sample dependencies
$config = require __DIR__.'/config.php';
$db = createDBConnection($config['db']);

# arguments you pass here get forwarded to the route actions
dispatch($db, $config);

If you want to see where all the state goes, you can use action, serve, and render instead.

<?php

require 'path/to/dispatch.php';

# create a stack of actions
$routes = [
  action('GET', '/books.json', function ($db, $config) {
    $list = loadAllBooks($db);
    $json = json_encode($list);
    return response($json, 200, ['content-type' => 'application/json']);
  }),
  action('GET', '/books/:id', function ($args, $db) {
    $book = loadBookById($db, $args['id']);
    $html = phtml(__DIR__.'/views/book', ['book' => $book]);
    return response($html);
  }),
  action('GET', '/about', page(__DIR__.'/views/about'))
];

# sample dependencies
$config = require __DIR__.'/config.php';
$db = createDBConnection($config['db']);

# we need the method and requested path
$verb = $_SERVER['REQUEST_METHOD'],
$path = $_SERVER['REQUEST_URI'],

# serve app against verb + path, pass dependencies
list($content, $status, $headers) = serve($routes, $verb, $path, $db, $config);

# flush out content, status code, and http headers
render($content, $status, $headers);

tests

Tests for dispatch use plain assertions.

php tests/dispatch-tests.php

If no errors were echoed, then you're all good.

breaking changes from 8.x

removed the following:
- attachments()
- blanks()
- config()
- cookies()
- ent()
- error()
- headers()
- hook()
- input()
- ip()
- json()
- map()
- nocache()
- session()
- stash()
- status()
- url()

added the following:
- action() - creates a route handler
- response() - returns a response tuple
- match() - matches the method + pattern against a list of actions
- serve() - performs match() against actions list, and invokes the
	match, and returns the resulting response tuple
- page() - creates an action that renders the file, like inline phtml()
- render() - renders the http response
- route() - creates an action and puts it into the context singleton
	(replaces the old map function)
- context() - returns the context singleton

changed the following:
- redirect() - now just returns a redirect response tuple
- phtml() - now requires the full path (minus extension) of phtml file,
	and no longer accepts a layout file

license

MIT

dispatch's People

Contributors

noodlehaus avatar nmcgann avatar scan avatar lloydzhou avatar dasrecht avatar kanti avatar oschettler avatar rmasters avatar devside avatar yalp avatar martinaglv avatar 4d47 avatar wtmissions avatar kafene 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.