Skip to content

Commit

Permalink
Added working example with file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
imagentleman committed May 18, 2015
1 parent 850fbd3 commit a5a4cc9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Routes match one to one to controllers and actions.
In this url:

http://example.com/watch/video/id

`watch` is the controller, `video` is the action and `id` is a parameter for the action (you can have multiple parameters like `http://example.com/watch/video/id/param2/param3`).

## Controllers
Expand All @@ -58,15 +58,15 @@ For the previous examples, we would have a `/controllers/watch.php` file like th
// some php code
}
}

or like this:

Class Watch extends Controller {
static function video($id, $param2, $param3) {
// some php code
}
}

The route, file name and class name must be the same (in this case `watch`).

## Views
Expand All @@ -75,29 +75,29 @@ Views are just php files with embedded html.

You could have a view located in `/views/hello_view.php` with something like this:

<p><?= 'hello world';></p>
<p>Hello World</p>

And you would use it from a controller like this:

Class Watch extends Controller {
static function video($id) {
echo Watch::view('hello_view');
echo Watch::view('hello_view');
// echo Controller::view('hello_view') you can also use the parent controller class
}
}

You can send variables to the view, so a view like this:

<p><?= "The video is $video_id";></p>

Would be called from the controller like this:

Class Watch extends Controller {
static function video($id) {
echo Watch::view('hello_view', array('$video_id' => $id));
}
}

## Models

Models use PDO with parametrized sql (parameters are optional). Only one function is available, named 'exec'.
Expand All @@ -112,14 +112,14 @@ A model in `/models/Watch_Model.php` would look like this:
return $this->exec('SELECT * FROM videos WHERE id = :video_id', array(':video_id' => $id));
}
}

And would be called from a controller like this:

Class Watch extends Controller {
static function video($id) {
$watch_model = Watch::model('Watch_Model'); // you can also use Controller instead of Watch
$video = $watch_model->get_video($id);
echo Watch::view('hello_view', array('video' => $video));
echo Watch::view('hello_view', array('video' => $video));
}
}

Expand All @@ -140,4 +140,4 @@ Redirects every request to index.php (except for files, like your css or js) and
Acknowledgements
----------------

This is heavily inspired by [PIP](https://github.com/gilbitron/PIP).
This is heavily inspired by [CodeIgniter](https://github.com/bcit-ci/CodeIgniter) and [PIP](https://github.com/gilbitron/PIP).
4 changes: 2 additions & 2 deletions config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$cfg['controller_main'] = ''; // Default controller (for the route "/")
$cfg['view_error'] = ''; // Default view for 404 errors
$cfg['controller_main'] = 'hello'; // Default controller (for the route "/")
$cfg['view_error'] = '404_view'; // Default view for 404 errors

$db['adapter'] = 'mysql'; // The pdo driver (e.g. "mysql")
$db['host'] = '';
Expand Down
17 changes: 17 additions & 0 deletions controllers/hello.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

Class Hello extends Controller {
static function index() {
echo Hello::view('hello_view',
array('message' => 'Hail Hyrdra!',
'link' => '/hello/say/hello',
'link_text' => 'Say Hello'));
}

static function say($message='') {
echo Hello::view('hello_view',
array('message' => $message,
'link' => '/hello',
'link_text' => 'Go Back'));
}
}
2 changes: 2 additions & 0 deletions views/404_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<p>404</p>
3 changes: 3 additions & 0 deletions views/hello_view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html>
<p><?= $message; ?></p>
<a href="<?= $link; ?>"><?= $link_text; ?></a>

0 comments on commit a5a4cc9

Please sign in to comment.