Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more work done

  • Loading branch information...
commit 48bcfcb908b409fb373e6990fa06b4e6db95ee77 1 parent bd0d1d7
joel hansson qzio authored
Showing with 51 additions and 27 deletions.
  1. +12 −12 README.md
  2. +1 −3 bootstrap.php
  3. +4 −2 lib/helpers.php
  4. +34 −10 lib/restish.php
24 README.md
View
@@ -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';
}
@@ -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>
@@ -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 bootstrap.php
View
@@ -1,5 +1,6 @@
<?php
define('BOOTSTRAP_DIR', dirname(__FILE__));
+session_start();
require BOOTSTRAP_DIR.'/config.php';
function require_libs($path = '')
@@ -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 lib/helpers.php
View
@@ -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;
@@ -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 lib/restish.php
View
@@ -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()
@@ -27,6 +27,11 @@ function request_method()
else return 'unkown';
}
+function redirect($uri) {
+ header('location: ' . $uri);
+ exit;
+}
+
function run( $controller )
{
if ( defined('USE_REWRITES') ) {
@@ -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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.