Skip to content

Getting Started for Beginners

Zhi edited this page Jul 19, 2014 · 2 revisions

The first Step

The usage is very simple. Just present a file named 'index.php'(or other files that you like) to bootstrap the whole helper classess/functions.

require('gtf.php');

poweredByGtf(array(
  'viewDir' => 'view',
  'dbConfig' => 'db.php'
));

The viewDir is the folder that contains the view layer of the website. You can use relative path or absolute path. Parameter dbConfig is the file that configures the database.

A sample dbConfig could be found here:

<?php if (!defined('GTF_PHP')) die(); return (object) array(

  'dsn' => 'sqlite:test.db',
  'username' => '',
  'password' => ''

);

This configuration will use a embedded SQLite database which is shipped with PHP release and use for convenience. The framework follow the convension of PDO. Of couse you have to prepare the database. :)

In the viewDir, design a template for some similar pages. Here we named it 'base.php'.

<?php if(!defined('GTF_PHP') die(); ?>
<!doctype html>
<html>
  <head>
    <title><?php Tpl::usePart('title') ?></title>
  </head>
  <body>
    <?php Tpl::usePart('body') ?>
  </body>
</html>

In this template, we drafted a basic html file with two placeholder title and body. Then write a page to display some data and fill in the template. We named the file 'hello.php'.

<?php if (!defined('GTF_PHP')) die(); ?>
<?php $data = Stq::query('select * from test'); ?>

<?php Tpl::base('base.php') ?>

<?php Tpl::part('title') ?>
Hello, these are the data.
<?php Tpl::part() ?>

<?php Tpl::part('body') ?>
<h1>Data</h1>
<ul>
  <?php foreach ($data as $datum): ?>
  <li><?= $datum['name'] ?></li>
  <?php endforeach; ?>
</ul>
<?php Tpl::part() ?>

Stq::query(...) is a wrapper around the PDO object. It will help you take care of the database connection and return the result dataset of the specific SQL. Other part of the file just fill in the parts of the base template specified by Tpl::base(...).

Finally, start your server like Apache HTTPd, and then open a web browser with URL http://localhost/index.php/hello and test it.

To explore more about the framework, see the source or wiki. Enjoy the journey. :)

Clone this wiki locally