Skip to content

soska/microbus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microbus

Completely original nano-framework for PHP (ok, a bit inspired by sinatra.rb)

Microbus is a tiny framework for creating PHP applications very easily. Altough it’s a bit inspired by “sinatra.rb”http://sinatrarb.com it does not try to mimic ruby’s programming style. Au contraire, Microbus is very PHP-ish (I’ll let you decide if that is a good thing or not) you decide if you want to use functional programming or OOP or a combination of both.

Microbus asumes nothing and imposes nothing. Just drop this download somewhere in your webroot and you are ready to go.

*Note: For this examples, I’ll asume that you installed it in http://localhost/microbus

Über-Simple Usage:

If you only need a pseudo-static site that consists only of some pages, you can just use views, with no logic at all. Fore example, just add this to application.php:


<?php 
include('lib/microbus.php');
Microbus::ride();
?>	

Then add a file named hello.php in the views folder with a code like this:


	
<h1>Hello World</h1>

Then go to http://localhost/microbus/hello in your browser and you’ll get your page. If you view the HTML source code, you’ll notice that the template output was inserted on a layout. That layout is in views/layouts/default.php

That’s it. If you don’t need fancy stuff like orm processing, routes, filters or logic, this is all you need to do.

Simple Route Functions:

Microbus::ride() detect’s the path and the HTTP method by which the user has come and redirects them to a function that is declared in this form: [httpverb]_[path]().

For example: Let’s say you need a basic contact form, in which case you’ll need to divide the task in to steps. First, present the form and second, process the form data.

Add this to application.php


<?php
include('libe/microbus.php');

function get_contact(){
	View::render('contact');
}

function post_contact(){
	
	// Retrieve the form's submitted data 
	$data = Microbus::request('data');
	
	// do something with the form data here.
	// ...
	// then redirect to the form view.
	Microbus::redirect();
}

Microbus::ride();

?>			

The contact view, is a simple HTML form which sent its data via a POST method.


	<form method="post">
		<label for="contact[email]">Your E-mail</label>
		<input type="text" name="contact[email]" value="">
		<label for="contact[email]">Your comment</label>
		<textarea name="contact[email]" value="" id="email" rows="8"></textarea>
		<input type="submit" value="Contact" >
	</form>		

If you prefer writing classes over simple functions you can declare an Application class and use method instead of functions.

	
<?php
class Application{

	function get_contact(){
		View::render('contact');
	}

	function post_contact(){
		$data = Microbus::request('data');

		// do something with the form data here.

		// then redirect to the form view.
		Microbus::redirect();
	}
}
?>
	

Conditional routing

This is what i like the most about Microbus. You could write a very simple application just as a series of if statements, like this.

	
<?php
if (get('/hi')) {
	// no params, just render a view.
	View::render('hi');
}
?>
	

You can also use named parameters.

	
<?php
if (get('/hi/:name')) {
	$named = Microbus::request('named');
	$sayHiTo = $named['name'];
}

if (get('/hi/:from/:to')) {
	$named = Microbus::request('named');
	$saysHi = $named['from'];
	$sayHiTo = $named['to'];
}
?>
	

Or a wildcard * to match everything into the params variable.

	
<?php
if (get('/hi/*')) {
	$everybody = Microbus::request('params');
	// This will match this:  'http://localhost/hi/hugo/paco/luis/mimi/'
	// and $everybody will be an array containing all that names.
}
?>		
	

What about the other verbs? Obviously besides get(), there’s also post(), put() and delete() available.

Callback Routing

If your app starts getting bigger, maybe you don’t like having dozens of conditional statements all over the place. That’s cool, get(), post() etc, accept a callback as the second parameter so you can separate your routing from your functions.

	

// the route declaration
get('hi/*','greetings');

// the callback function
function greetings($from, $to){
	
	// the url params get passed to the function, of course you can still use
	// Microbus::request('params'); and Microbus::request('named'); if you prefer it.

	// btw, you can set the page title like this:
	View::set('pageTitle','Greetings from '.$from);

	// and pass variables to the view like this:
	View::set(compact('from','to'));
	
	View::render('greetings');	
}
		
	

Isn’t that nice? But it can look better if you’re using PHP 5.3’s lambda functions.

	
<?php
get('hi/*',function($from,$to){
	View::set('pageTitle','Greetings from '.$from);
	View::set(compact('from','to'));
	View::render('greetings');		
});
?>		
	

Awww.

That’s pretty much it.

There are some nice stuff behind the scenes and I’m working on adding the ability to make Microbus plugins, but there’s no model or database abstraction layer and all that stuff. You’ll have to add it yourself.

I know that this seems like no more that a glorified router, but that’s the beauty of Microbus. Sometimes big frameworks like CakePHP or Symfony are overkill for small applications. Or maybe you don’t really like that convention over configuration stuff and prefer to do things either your way or the highway.

Microbus is the highway.

About

A nano framework for simple PHP apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages