Caw - a small lightweight response system for PHP
PHP
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Corvum
README.md
composer.json

README.md

Caw

Caw - a small lightweight response system for PHP

##Installation

###Install with Composer

If you're using Composer to manage dependencies, you can add Caw with it.

{
    "require": {
        "corvum/caw": "dev-master"
    }
}

##Usage

###Setup

require('/vendor/autoload.php');

###Basic Usage

$caw = new \Corvum\Caw([
    'viewsDir' => dirname (__FILE__).'/views/', //Directory for views
    'view' => 'index' //Default View
]);
$caw->siteTitle = 'Corvum';
$caw->siteLead = 'Easy to Use PHP Tools';
$caw->setView('index');  //index.php in the views directory
$caw->respond();

###API / JSON Usage

$caw = new \Corvum\Caw([
    'responseType' => 'JSON'
]);
$caw->siteTitle = 'Corvum';
$caw->siteLead = 'Easy to Use PHP Tools';
$caw->respond();

OR

$caw = new \Corvum\Caw();
$caw->siteTitle = 'Corvum';
$caw->siteLead = 'Easy to Use PHP Tools';
$caw->respond('JSON');

####Enable Cors

To be able to use this as a true API from other domains, you will need to enable CORS.

$caw = new \Corvum\Caw([
    'responseType' => 'JSON',
    'enableCORS' => true
]);

OR

$caw->enableCORS();

###Setting Cookies

Setting Cookies is done just as you normally with setcookie(). $time defaults to '+1 day' but accepts anything strtotime() does.

$caw->setCookie($name, $value, $time);

The reason to use Caw's setCookie method is to ensure that everything is sent to the server in the proper order for the HTTP Response. If you ensure that you use Caw as the only sending data down, your headers, cookies and content will always work. That is a reason I created Caw, to keep from accidentally starting to send something back, and cookies or headers didn't work because they weren't in the correct order

###Setting Headers

$caw->setHeader('Content-Type', 'application/json');

###HTTP Status Codes

$caw->setStatusCode('404','Not Found');

###Render Data

Sometimes it's useful to just render the data and return that back to the application. To do that with Caw, you just need to use render() instead of respond()

$caw->render();

###Stop

Stop allows you to exit your application while still sending down what view/data you have already set. This ensures that you can exit either gracefully (by setting a default view) or send empty responses with status code (no view/data)

$caw->stop();