Welcome to Cosmos

This document helps you to create Web application soon by Cosmos.
Cosmos works on most Unix, Linux, Mac, and Windows systems as long as PHP is available on this platform.

PHP Development Environment


If you don't have PHP development environment before using Cosmos, This topic helps you to set up a PHP development environment on your computer.
Select a way in below by your choice.

Each components

You need to download and install for each of the below componments.

Using XAMPP(Easy development package)

This can easily set up your computer by using XAMPP.

Installation


There are some ways to get Cosmos modules as following.

Command Line Installation

git clone https://git01.codeplex.com/cwf /path/to/Cosmos

Download the archived copy


Configuration


Setup

  • Document root
    • Point your document root to the htdocs/ directory in Cosmos modules.
    • For example, to use Apache HTTP Server, consult the following.
httpd.conf
DocumentRoot "/path/to/Cosmos/htdocs"
~
Directory "/path/to/Cosmos/htdocs"
  • Permissions
    • Cosmos uses the logs/ directory for writing log system.
    • If using the chmod command with ssh, consult the following.
# chmod 707 /path/to/Cosmos/logs

Check The working

Run your browser and enter the following URL: http://localhost/index.html you will show phpinfo.
By the following setting that maps URLs, you can add and see new html page.

/path/to/Cosmos/htdocs/i.php
$dispacher = new Cosmos\Dispatcher();
$dispacher->hook(".*", 'Cosmos\Template::handle');

If you see adding html page, point the URI according to the directory of httdocs.
/path/to/Cosmos/htdocs/hello.html
<h1>{% Hello::call(); %}</h1>
/path/to/Cosmos/modules/hello.php
<?php
class Hello {
    public static function call(){
        echo 'Hello World';
    }
}

helloworld2.png

Supported database

  • It is expected to work with any database that PDO supports.
  • Point the dsn of using database.
  • For example, to use SQLite, consult the following and pdo drivers

/path/to/Cosmos/conf/quick.php
// $_PDOENVS[ dsn ][ user, password, options ]
$_PDOENVS['sqlite:/mydb'] = array();

Tutorial : Making Datastore API


This tutorial will show you creation of a simple data store API.
This API system depends on the HTTP method, which processes.
  • HTTP:POST:add record to the appdata table
  • HTTP:GET:get record from the appdata table
  • HTTP:PUT:update record from the appdata table
  • HTTP:DELETE:delete record from the appdata table
Create a directory named "modules" for the new modules.

Database method

If you haven't made database, create an empty database.
Then, create a single table for the data store system.
Execute the following SQL statements into your database

CREATE TABLE appdata (
   id     INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
   value  TEXT NOT NULL
);

Create modules that interact with the database.

/path/to/Cosmos/modules/appdata.php
<?php
class Appdata {
    use Cosmos \ Database \ Method; //This class is for calling the SQL.Please define.
    use Cosmos \ Factory; //This class is to call the Service from View.Please define.
}

Create a module under a directory named appdata.
Then create a SQL file to be used with appdata.php.

/path/to/Cosmos/modules/appdata/create.sql
INSERT INTO `appdata`(:?) VALUES (:*);

/path/to/Cosmos/modules/appdata/read.sql
SELECT * FROM `appdata` WHERE =:id;

/path/to/Cosmos/modules/appdata/update.sql
UPDATE `appdata` SET =:value WHERE =:id;

/path/to/Cosmos/modules/appdata/delete.sql
DELETE FROM `appdata` WHERE =:id;

Routes and Actions

Set url to i.php for a routes.
In this case, Set the Actions of the Datastore that matches the HTTP method to the URI:~/datastore.
Next delete setting for template and write setting for header of json, because this system is only json format,

/path/to/Cosmos/htdocs/i.php
$dispacher = new Cosmos\Dispatcher();
$dispacher->hook("\/datastore$", 'Cosmos\URL::handle');

Create new modules for the processing of each action.

/path/to/Cosmos/modules/datastore.php
<?php
class Datastore  {
    /**
     * HTTP GET:get records from the appdata table
     * By Annotation of Http\Request, you can get request query as arguments.
     *
     * @param int $id
     * @\Cosmos\Http\Request
     */
    public function get($id) {
        // calling SQL statements from 'appdata/read.sql'
        $res = Cosmos \ Assert :: is(Appdata :: read(array('id'=>$id)), !empty($id), 'the request was invalid')->fetch(PDO::FETCH_ASSOC);
        header('Content-Type: application/json');
        echo json_encode($res?:array());
    }

    /**
     * HTTP POST:add record to the appdata table
     * By Annotation of Http\Request, you can get request query as arguments.
     *
     * @param string $value
     * @\Cosmos\Http\Request
     * */
    public function post($value) {
        // calling SQL statements from 'appdata/create.sql'
        Cosmos \ Assert :: is(Appdata :: create(array('value'=>$value)), !empty($value), 'the request was invalid');
    }

    /**
     * HTTP PUT:update record from the appdata table
     */
    public function put() {
        Cosmos \ Http :: body($req);
        // calling SQL statements from 'appdata/update.sql'
        Cosmos \ Assert :: is(Appdata :: update($req), isset($req['id']) && isset($req['value']), 'the request was invalid');
    }

    /**
     * HTTP DELETE:delete record from the appdata table
     */
    public function delete() {
        Cosmos \ Http :: body($req);
        // calling SQL statements from 'appdata/delete.sql'
        Cosmos \ Assert :: is(Appdata :: delete($req), isset($req['id']), 'the request was invalid');
    }
}

That's it!

Run the HTTP mehotd, depending on the operation of appdata table.

Add record(HTTP:POST)

POST /datastore HTTP/1.1
Host: localhost

value=hello

HTTP/1.1 200 OK

Get record(HTTP:GET)

GET /datastore?id=1 HTTP/1.1
Host: localhost

HTTP/1.1 200 OK
Content-Type: application/json

{"id":"1","value":"hello"}

Update record(HTTP:PUT)

PUT /datastore HTTP/1.1
Host: localhost

id=1&value=helloagain

HTTP/1.1 200 OK

Delete record(HTTP:DELETE)

DELETE /datastore HTTP/1.1
Host: localhost

id=1

HTTP/1.1 200 OK

Last edited Mar 4, 2014 at 11:27 AM by shogirin, version 51

Comments

No comments yet.