Getting Started - SergiJuanola/Toolbox GitHub Wiki

Do you want to start a new project with Toolbox? In that case, just download the latest copy of Toolbox, uncompress it into a folder (in terms of organisation, it should be stored outside the public_html folder). For our convenience, we will call this folder /toolbox, and we will store one level up from our root:

.
├── public_html
│   └── index.php
└── toolbox
    ├── class.base.php
    ├── class.brush.php
    └── ...

Open your website root and write the following in an index.php file:

<?php 
require_once '../toolbox/class.toolbox.php';
if(!defined("__DIR__")) define("__DIR__", dirname(__FILE__));

$app = Toolbox::build(array(
    'match'     =>  array(
                        'matchbox'  =>  __DIR__.'/matchbox/'
                    ),
    'brush'     =>  array(
                        'views' =>  __DIR__.'/views/',
                        'layout' => 'layout.php',
                    )
  ));

This creates a Toolbox instance and adds some configuration for Match and Brush. You could also add configuration for Orm, Dictionary or any other tool, or none of them, if you wish. As we are keeping it simple, we will only add these two, so we can create a friendly url website with templates. You should also create an .htaccess to redirect everything to index.php:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Then, you can create a Match instance to start url matching:

<?php
// following index.php...
$match = Match::build()
->matchAny('/', 'Home::index')
->fire();

In this case, we are matching the home screen, and sending its request to the HomeController, which is a class named controller.home.php and stored inside /matchbox. Then, we call index() within HomeController. This is an example of HomeController, hosted inside /matchbox, and holding only a method:

<?php 

require_once('../toolbox/class.controller.php');

class HomeController extends Controller {
	public function index()
	{
		Brush::build()->paint('home.php', array('title'=>'My awesome Homepage', 'name'=>'John'));
	}
}

As you will find out, that parameter added to $match called 'Home::index' targets the HomeController's index action.

Once Match matches the current url with any rule (most of the cases, the first parameter), it will call the second parameter, crawling for a controller and an action that fits that callback. If no rule is found matching the current url, Match fires a 404 status code.

In case anyone enters to yourdomain.com/, the first (and currently only) rule will trigger the index() method in HomeController. Then, it is Brush's turn to do its job.

Brush is a simple template renderer for Toolbox (and actually any other system). It is based in three principles:

  1. It paints/renders the view from the first parameter. It looks inside the folder written in views param from Brush.
  2. It passes any variable inside the associative array from the second parameter, and it will be usable either in the view or the layout. For terms of templating, no variable should be named $content, as it is used for Brush to print the view inside the layout.
  3. PHP is powerful enough to work as a template engine. Use PHP to write your layouts, and avoid learning other templating languages.

Here are the files we used to render the page. layout.php:

<html>
<head>
	<title><?php echo $title ?></title>
</head>
<body>
<?php echo $content ?>
</body>
</html>

And home.php:

Hello <?php echo $name; ?>

The final structure for our example website was:

.
├── public_html
│   ├── matchbox
│   │   └── controller.home.php
│   ├── views
│   │   ├── home.php
│   │   └── layout.php
│   ├── .htaccess
│   └── index.php
└── toolbox
    ├── class.base.php
    ├── class.brush.php
    └── ...

Those folders don't need to be named like that. Actually, you can put the folders anywhere you want, if you configure it inside Toolbox or during the tool building.

⚠️ **GitHub.com Fallback** ⚠️