Skip to content

eimg/tinymvc

Repository files navigation

TinyMVC

TinyMVC is a PHP micro framework with MVC router and responsive web design boilerplate, especially designed to quickly bootstrap web apps without unnecessary overhead and complexity. The structure and code are construct in simple imperative manner so that developer can get full understanding and control over base framework as well as the app.

Requires Apache mod_rewrite and PHP short open tags.

Getting Start

config.php

Set the default controller and database setting in config.php. TinyMVC currently supporting two database driver, MySQL and Sqlite.


$config = array(
	## Sqlite
	"dbdriver" => "sqlite",
	"db" => "data/db.sqlite",

	## MySQL
	# "dbdriver" => "mysql",
	# "dbhost" => "127.0.0.1",
	# "dbuser" => "root",
	# "dbpass" => "",
	# "dbname" => "tinymvc",

	"default-controller" => "home"
);

router.php

Set the route pattern in router.php


$route = array("controller", "action", "id");

The default route map will handle the request in controller/action/id pattern. controller always should be the first value in route map.

Model-View-Controller

Controllers

Controllers are the key and most important components. Each request go to respective controller. For example, if the request is http://example.com/home/add/123, that will reach to home controller with the additional parameters, add/123. Since the route map is controller/action/id, "add" would be "action" and "123" would be "id" in this case.

Controllers files should stored in controllers directory. There are two example controllers included (home.php and todo.php). Take a look at home controller for view-template based apps. Take a look at todo controller for API apps.

In default configuration, home has been set as "default-controller". So, if there is no controller pointed in request URL, home will be use as default controller.

redirect() function can be use to redirect through router pattern. For example, redirect("home/contact") will immidiately redirect to http://example.com/home/contact/.

Views

Views are optional and not necessary in API apps. But, it's useful for template based apps. Each template set should be store in views directory by organization with sub-directory that has the same name with controller.

Use render() function to wrap your template with template wrapper. You can pass the values to template through $data array.


$data['one'] = "value one";
$data['two'] = "value two";

render("home", $data);

In view, each index become variable and variables $one and $two would be available in this case. For security, you may use f() function for outputs, which filter potential XSS script.

Main wrapper HTML template is stored as template/index.php. Other necessary static resources should also store in respective directories under template directory.

Models

Models are also optionals. A model file should have same name with controller and should be store in models directory. TinyMVC will use the models if exists.

Controls

When you need to add CSS Link, JS Source, Hyperlinks, Images and Forms to your view template, you should use build-in controls instead of raw HTML. Take a look at template/index.php for example. You might also want to check available controls in includes/controls.php. And, you may extend it as you like for more richer control-set.

REST Helper - Updated in build 20140817

TinyMVC now has additional REST helper functions respond(), template() and http_auth(). The purpose of respond() is to handle custom response header and content-type.


respond($data, $status_code, $content_type);

$data should be array. $status_code is optional and the default status code is 200 OK. Please see includes/core.php for supported status codes. $content_type is also optional and default content type is JSON. Supported content types are JSON, HTML, Plain Text and JavaScript.

The purpose of template() is to respond pre-defined template (unlike render(), without wrapper) to API requests. Create pre-defined template files in views/templates/.

The purpose of http_auth() is to provide standard HTTP Authentication. Call it on top of API controller to add simple API authorization. Please don't forget to change username/password list. See the http_auth() function at includes/core.php.

Action Map

TinyMVC also has action maping mechanism. To create an action map, create a map file in controllers. For example, if your controller file name is api.php, add api.map.php to define action map. Following is the syntax:


$GET = array (
	"greet" => "hello",
	"leave" => "bye"
);

$POST = array (
	"foo" => "bar"
);

For example, by given sample above, TinyMVC will invoke hello() function if the request method is GET and action parameter is greet. Just don't forget to define hello() function in controller.

* Manual action mapping is required due to security reason instead of directly tying request action to function.

More

For more information, view the example controllers and other source codes. For example, you can check for available database methods in database wrapper class located in includes/db.php.

Download

Source Code: https://github.com/eimg/tinymvc/

Licenses

Tinymvc is license under MIT License. Please feel free to use, modify and redistribute as you wish.

About

PHP micro framework with MVC pattern to quickly bootstrap a REST app

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published