Permalink
Browse files

Initial commit: version 1.0.

  • Loading branch information...
0 parents commit fb46b5177c39613f8003eab677aa5bed86a65e42 Dave Hensley committed Apr 17, 2011
25 README
@@ -0,0 +1,25 @@
+PunyMVC is probably the most lightweight PHP MVC framework.
+It barely has any features at all. In spite of that, it's
+very useful for writing web apps, as it helps you keep your
+code organized in a logical way, but the framework itself
+never gets in your way. At all. Plus, there's really no
+learning curve (assuming you already understand the MVC
+paradigm and know how to code in object oriented PHP).
+
+The file public_html/index.php is really the entire framework.
+But a very, very simple blog demo app is included as well, to
+make it easier for you to see how to organize your code.
+
+Because most MVC frameworks (and web apps in general) use a
+relational database like MySQL, I decided to use MongoDB in
+the demo. The demo data is located in the included dump/
+directory. To use it, simply install MongoDB on your system,
+and use "mongorestore" to create the punymvc_demo database.
+
+Once you've loaded the data, all you need to do is move the
+contents of public_html (including the .htaccess file) into
+a web directory somewhere, and then access it through a
+browser. You can view a detailed walkthrough of setting up
+this demo app here:
+
+http://dave.ag/an-mvc-framework-in-799-bytes/
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,4 @@
+RewriteEngine On
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule . index.php [L]
@@ -0,0 +1 @@
+deny from all
@@ -0,0 +1,4 @@
+<?php
+
+define('MONGO_SERVER', 'mongodb://localhost:27017');
+define('MONGO_DATABASE', 'punymvc_demo');
@@ -0,0 +1 @@
+deny from all
@@ -0,0 +1,83 @@
+<?php
+/**
+ * Posts controller
+ *
+ * A really simple controller to demonstrate PunyMVC
+ *
+ * PHP versions 5
+ *
+ * @author Dave Hensley <davehensley@gmail.com>
+ * @package PunyMVC
+ */
+
+/**
+ * Posts controller
+ *
+ * A really simple controller to demonstrate PunyMVC
+ *
+ * @package PunyMVC
+ */
+class PostsController {
+
+/**
+ * The default layout used by this controller's views
+ *
+ * @access public
+ * @var string
+ */
+ public $layout = 'simple';
+
+/**
+ * The model used by this controller
+ *
+ * @access private
+ * @var object
+ */
+ private $Posts;
+
+/**
+ * Constructor
+ *
+ * This method initializes the PostsController object.
+ *
+ * @access public
+ */
+ public function __construct() {
+ require_once 'models/posts.php';
+ $this->Posts = new PostsModel();
+ }
+
+/**
+ * Index action
+ *
+ * The default action when no action is specified in the URL.
+ *
+ * @access public
+ * @return array The variables that get passed to the view as $vars
+ */
+ public function index() {
+ $posts = $this->Posts->find();
+ return array('posts' => $posts);
+ }
+
+/**
+ * View action
+ *
+ * Displays a single Post
+ *
+ * @param array $args The arguments passed through from the URL
+ * @return array The variables that get passed to the view as $vars
+ */
+ public function view($args) {
+ if (ctype_digit($args[0])) {
+ $post = $this->Posts->find(array('_id' => intval($args[0])));
+ }
+
+ if (!isset($post) || empty($post)) {
+ include 'pages/404.php';
+ die();
+ }
+
+ return array('post' => $post[$args[0]]);
+ }
+}
@@ -0,0 +1,74 @@
+<?php
+/**
+ * PunyMVC: A very, very lightweight MVC framework
+ *
+ * PHP version 5
+ *
+ * Copyright (c) 2011 Dave Hensley
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @author Dave Hensley <davehensley@gmail.com>
+ * @copyright Dave Hensley 2011
+ * @link http://dave.ag/an-mvc-framework-in-799-bytes/
+ * @package PunyMVC
+ * @version 1.0
+ */
+
+function load_view($controller, $action, $vars) {
+ if (is_file("views/$controller/$action.php")) {
+ ob_start();
+ include "views/$controller/$action.php";
+ $content = ob_get_contents();
+ ob_end_clean();
+ }
+
+ return isset($content) ? $content : '';
+}
+
+function render_page($content, $layout) {
+ if (isset($layout)) {
+ include "layouts/$layout.php";
+ } else {
+ echo $content;
+ }
+}
+
+include 'config/config.php';
+$baseDirectoryLength = strlen(dirname($_SERVER['PHP_SELF']));
+$parsedURI = explode('/', substr($_SERVER['REDIRECT_URL'], ($baseDirectoryLength > 1) ? $baseDirectoryLength + 1 : 1));
+
+if (!($controller = $parsedURI[0])) {
+ include 'pages/index.php';
+ exit();
+} elseif (!preg_match('/\W/', $controller . isset($parsedURI[1]) ? isset($parsedURI[1]) : '') && is_file("controllers/$controller.php")) {
+ include "controllers/$controller.php";
+ $controllerClass = $controller . 'Controller';
+ $action = isset($parsedURI[1]) && !empty($parsedURI[1]) ? $parsedURI[1] : 'index';
+
+ if (class_exists($controllerClass) && in_array($action, get_class_methods($controllerClass))) {
+ $controllerObject = new $controllerClass;
+ $content = load_view($controller, $action, $controllerObject->$action(array_slice($parsedURI, 2)));
+ render_page($content, isset($controllerObject->layout) ? $controllerObject->layout : null);
+ exit();
+ }
+}
+
+header('HTTP/1.1 404 Not Found');
+include 'pages/404.php';
@@ -0,0 +1 @@
+deny from all
@@ -0,0 +1,7 @@
+<html>
+ <head>
+ </head>
+ <body>
+ <?php echo $content ?>
+ </body>
+</html>
@@ -0,0 +1 @@
+deny from all
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Posts model
+ *
+ * A really simple model to demonstrate PunyMVC
+ *
+ * PHP versions 5
+ *
+ * @author Dave Hensley <davehensley@gmail.com>
+ * @package PunyMVC
+ */
+
+/**
+ * Posts model
+ *
+ * A really simple model to demonstrate PunyMVC
+ *
+ * @package PunyMVC
+ */
+class PostsModel {
+
+/**
+ * The Mongo object
+ *
+ * @access private
+ * @var object
+ */
+ private $mongo;
+
+/**
+ * The MongoDB object
+ *
+ * @access private
+ * @var object
+ */
+ private $mongoDb;
+
+/**
+ * The MongoCollection object
+ *
+ * @access private
+ * @var object
+ */
+ private $posts;
+
+/**
+ * Construct method
+ *
+ * This method initializes the PostsModel object
+ *
+ * @access public
+ */
+ public function __construct() {
+ $this->mongo = new Mongo(MONGO_SERVER);
+ $this->mongoDb = $this->mongo->{MONGO_DATABASE};
+ $this->posts = $this->mongoDb->posts;
+ }
+
+/**
+ * Find method
+ *
+ * This method simply executes a find() on a MongoCollection
+ *
+ * @access public
+ * @param array $query The MongoDB query
+ * @param array $fields The list of fields to return
+ * @return array The resulting data
+ */
+ public function find($query = array(), $fields = array()) {
+ # Very simple example, we just pass the query straight to Mongo
+ return iterator_to_array($this->posts->find($query, $fields));
+ }
+}
@@ -0,0 +1 @@
+deny from all
@@ -0,0 +1 @@
+Page not found.
@@ -0,0 +1 @@
+Index Page
@@ -0,0 +1 @@
+deny from all
@@ -0,0 +1,5 @@
+Here are the posts:<br /><br />
+<?php foreach ($vars['posts'] as $post) : ?>
+ <?php $post_uri = sprintf('%s/posts/view/%u', dirname($_SERVER['PHP_SELF']), $post['_id']) ?>
+ <a href="<?php echo $post_uri ?>"><?php echo $post['title'] ?></a> by <?php echo $post['author'] ?><br />
+<?php endforeach ?>
@@ -0,0 +1,6 @@
+This is a single post:<br />
+<br />
+Title: <?php echo $vars['post']['title'] ?><br />
+Author: <?php echo $vars['post']['author'] ?><br />
+Content:<br />
+<?php echo $vars['post']['content'] ?><br /><br /><br />

0 comments on commit fb46b51

Please sign in to comment.