Skip to content

Commit

Permalink
more work done
Browse files Browse the repository at this point in the history
  • Loading branch information
qzio committed Apr 5, 2011
1 parent bd0d1d7 commit 48bcfcb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,31 @@ everything in lib/ is required automatically, just drop what ever libs/ or modul
lib/ # everything here is auto required by bootstrap.php
| - helpers.php # default helpers
| - restish.php # defines the is_get is_post is_put is_delete functions..
| - db.php # static methods that wrapps pdo
| - other library, helper and model files


# TODO
"With rewrite" is the preffered way to go if possible, nicer urls is also a nice addition ;)
- see rewrite rule for different http deamons at the bottom of this readme


# some examples with comments

## controllers/hello.php (controller)
## public/hello.php (controller)

<?php
require '../bootstrap.php';

function on_before() {
function on_before( $params = array() ) {
// before filter
return $params;
}

function on_get( $params = array() ) {
$p['title'] = 'Hello World';

if ( $_GET['name'] ) {
$_GET['name'] = 'Unknown';
if ( $params['name'] ) {
$p['name'] = $params['name'];
} else {
$p['name'] = 'Unknown';
}
Expand All @@ -63,8 +64,10 @@ everything in lib/ is required automatically, just drop what ever libs/ or modul

}

run(basename(__FILE__,'.php')); // this is the dispatcher

## templates/hello.html.php (template)

## templates/hello.php (template)

<h1><?=h( $p['title'] )?></h1>
<p>
Expand All @@ -77,23 +80,20 @@ everything in lib/ is required automatically, just drop what ever libs/ or modul
* and add a <input type="hidden" name="_method" value="put"/> in order return correct method.
*/
?>
<?= form_start(array('method'=> 'put')) ?>
<?= form_start(array('method'=> 'get')) ?>
<fieldset>
<input type="text" name="name"/>
<input type="submit"/>
</fieldset>
</form>

## templates/header.php (auto included by public/render.php)
## templates/layout.php (autoincluded if the first argument to template() doesn't start with '

<!DOCTYPE html !>
<html>
<head>
<title><?=h( $p['title'] )?></title>
<body>

## templates/footer.php (auto included by public/render.php)

<?= $p['_content'];?>
</body>
</html>

4 changes: 1 addition & 3 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
define('BOOTSTRAP_DIR', dirname(__FILE__));
session_start();
require BOOTSTRAP_DIR.'/config.php';

function require_libs($path = '')
Expand All @@ -14,6 +15,3 @@ function require_libs($path = '')
}

require_libs(BOOTSTRAP_DIR.'/lib'); // require all the libraries recursevly

db::setConnectionInfo(cfg::db_name, cfg::db_user, cfg::db_password);
$p = array(); // use this var, it will be automatically extracted upon default render
6 changes: 4 additions & 2 deletions lib/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function h($str) {
* @return void
*/
function template($file,$params = array()) {
$use_layout = (substr($file,0,1) !== '_' && ! isset($params['_no_layout']) ) ? true : false;
$template_paths = BOOTSTRAP_DIR.'/templates';
$template = basename($file);
$render_me = $template_paths.'/'.$template;
Expand All @@ -36,6 +37,7 @@ function template($file,$params = array()) {
} else {
require $template_paths.'/error.php';
}
$content = ob_get_clean();
return $content;
$p['_content'] = ob_get_clean();
$p['_no_layout'] = true;
return ($use_layout) ? template('layout.php',$p) : $p['_content'];
}
44 changes: 34 additions & 10 deletions lib/restish.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ function is_get() {
}

function is_put() {
if (isGet()) return false;
if (is_get()) return false;
return (isset($_POST['_method']) && $_POST['_method'] == 'put');
}

function is_delete() {
if (isGet()) return false;
if (is_get()) return false;
return (isset($_POST['_method']) && $_POST['_method'] == 'delete');
}

function is_post() {
return ( is_get() || is_update() || is_delete() ) ? false : true;
return ( is_get() || is_put() || is_delete() ) ? false : true;
}

function request_method()
Expand All @@ -27,6 +27,11 @@ function request_method()
else return 'unkown';
}

function redirect($uri) {
header('location: ' . $uri);
exit;
}

function run( $controller )
{
if ( defined('USE_REWRITES') ) {
Expand All @@ -35,24 +40,43 @@ function run( $controller )

$params = $_REQUEST;

if (function_exists('on_before')) {
$params = on_before($params);
}

$output = '';

switch( request_method() ) {
case 'put':
if (function_exists('on_put')) $output = on_put($params);
if (function_exists('on_put'))
$output = on_put($params);
else
$output = template('error.php', array('error' => 'on_put is not defined for this resource'));
break;

case 'post':
if (function_exists('on_post')) $output = on_post($params);
if (function_exists('on_post'))
$output = on_post($params);
else
$output = template('error.php', array('error' => 'on_post is not defined for this resource'));
break;

case 'delete':
if (function_exists('on_delete')) $output = on_delete($params);
if (function_exists('on_delete'))
$output = on_delete($params);
else
$output = template('error.php', array('error' => 'on_delete is not defined for this resource'));
break;

case 'get':
if (function_exists('on_get')) $output = on_get($params);
if (function_exists('on_get'))
$output = on_get($params);
else
$output = template('error.php', array('error' => 'on_get is not defined for this resource'));
break;

default:
$output = template('error.php', array('error' =>'no request_method'));
$output = template('error.php', array('error' =>'unable to dispatch'));
}

return $output;
echo $output;
}

0 comments on commit 48bcfcb

Please sign in to comment.