Download the PHP package aloefflerj/yet-another-controller without Composer
On this page you can find all versions of the php package aloefflerj/yet-another-controller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aloefflerj/yet-another-controller
More information about aloefflerj/yet-another-controller
Files in aloefflerj/yet-another-controller
Package yet-another-controller
Short Description This is basically a http request and response controller using as much PSR's as it can (or just an excuse for personal study)
License mit
Informations about the package yet-another-controller
Yet Another PHP Controller
Here is how it works (for now):
Installation
composer require aloefflerj/yet-another-controller
Quick Usage
-
Simple Setup
index.php
-
Adding a simple GET route
index.php
-
Dispatching routes
index.php
-
Start your webserver
A simple way to do it would be with the PHP built-in web server:
Run
php -S localhost:8000 -t path/to/your/index
- Go to your domain home page
http://localhost:8000/
outputs: 'Welcome home'
Request and Response
The $request
and $response
arguments on closure
output are PSR-7 implementations.
The return of the closure
must be a StreamInterface
implementation for the output to work.
Since the return of $response->getBody()
is a StreamInterface
implementation the code above will print:
outputs: 'Welcome home'
Alternatively you could simply do echo $response->getBody();
Uri Params Usage
You can pass uri params wiht the syntax /route/{param}
.
For the param
to be accessed, you need a third param on get
method: \stdClass $args
.
To access it inside the closure
output, you must use $args->param
. See the example below:
url: localhost:8000/mascots/elephant
outputs: 'PHP elephant mascot is awesome'
Getting the Request Body
To get the body from a request you can access it from the $request
implementation with $request->getBody()
(a StreamInterface
implementation).
The example below will print the request body.
If I request a POST with the following body:
The return should be the same:
Obs: although you could try this controller out if you want, this project is just an excuse for me to study and have fun :v
All versions of yet-another-controller with dependencies
guzzlehttp/guzzle Version ^7.5
vishnubob/wait-for-it Version dev-master