First, let's set up our basic application.
As I previously mentioned, we're building this application using the Jolt micro framework, which is a mini MVC framework that I developed and used for many applications.
I'm going to go over a basic introduction to it so that you can see how it all works and then as we go through each recipe, we'll build on our application until we get a nice, handy system.
You can download the Jolt framework from http://joltframework.com/.
Jolt works in an interesting way; we set it up so that the get
and post
portions of the site are separated as shown in the following code snippet:
<?php include 'jolt.php'; $app = new Jolt('my app'), $app->get('/greet', function () use ($app){ // render a view $app->render( 'page', array( "pageTitle"=>"Greetings", "body"=>"Greetings world!" )); }); $app->post('/greet', function () use ($app){ // render a view $app->render( 'page', array( "pageTitle"=>"Greetings", "body"=>"Greetings world!" )); }); $app->get('/', function() use ($app) { $app->render('home'), }); $app->listen(); ?>
This will build a basic application that has an index and a greeting page. However, the greetings page shows both the
get()
and post()
methods, which means that a loading/greeting with get
(meaning, not called from a form) will result in one page, and a loading/greeting as a form submission will get you something entirely different. You can also add
route()
, if you don't care about get()
or post()
, or add put()
and delete()
.
Another useful feature of Jolt is the session store; if we called the $app->store("name","test");
method, we can call $name = $app->store('name'),
at any point in time and return the variable we assigned. This is useful for storing data that needs to be retrieved across the site, such as a logged-in user.
One thing you will notice is that instead of having multiple PHP files for each page, we created a new route.
The complete source code for this recipe can be found in the Chapter9/Recipe1
folder in the source code for this book.
This recipe will download the necessary pieces and set it up for our PBX.
We're going to set up the application using the following steps:
system
.jolt.php
, functions.php
, and pdo.class.php
to the system
folder..htaccess
file to your website.RewriteEngine On # RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L]
views
.views
called layout.php
with the following content:<html> <head> <title><?=$pageTitle?></title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.no-icons.min.css" rel="stylesheet"> <link href="//netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css" rel="stylesheet"> <link href="//netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome-ie7.css" rel="stylesheet"> <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-responsive.min.css" rel="stylesheet"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="masthead"> <h3 class="muted">My PBX</h3> <div class="navbar"> <div class="navbar-inner"> <div class="container"> <ul class="nav"> <li class="active"><a href="<?=$uri?>/">Home</a></li> <li><a href="<?=$uri?>/login">Login</a></li> <li><a href="<?=$uri?>/signup">Signup</a></li> </ul> </div> </div> </div><!-- /.navbar --> </div> <?=$pageContent?> <hr /> <div class="footer"> <p>© MY PBX <?=date("Y")?></p> </div> </div> <!-- /container --> </body> </html>
Services/
folder to your website.sql.sql
to your database.config.ini
to the site that contains the following content:;site settings site.name = my site site.url = ; rendering vars views.root = views views.layout = layout ; session vars cookies.secret = IeNj0yt0sQu33zeflUFfym0nk1e cookies.flash = _F ; twilio vars twilio.accountsid = twilio.authtoken = twilio.fromNumber = ; database vars mysql.dbhost = mysql.dbname = mysql.dbuser = mysql.dbpass =
You may notice this file is laid out slightly differently than your previous config.php
files because of the way this framework works. Instead of separate variables, we store them in an .ini
file that is read by the Jolt system store. Then, instead of calling $dbhost
, we will call $app->option('mysql.dbhost')
.
index.php
(containing the following content) to your site as follows:<?php include 'Services/Twilio.php'; require("config.php"); require("system/jolt.php"); require("system/pdo.class.php"); require("system/functions.php"); $_GET['route'] = isset($_GET['route']) ? '/'.$_GET['route'] : '/'; $app = new Jolt('site',false); $app->option('source', 'config.ini'), $mysiteURL = $app->option('site.url'), $app->get('/', function() use ($app){ $app->render( 'home' ); }); $app->listen();
home.php
file to your views
folder:<div class="jumbotron"> <h1>My PBX</h1> <p class="lead"> This is a basic PBX system built for the Twilio Cookbook </p> </div>
In steps 1, 2, 3, 4, and 5, we downloaded and installed the Jolt framework for PHP. We also set up a system
and views
folder.
In step 6, we created layout.php
, which is our layout for the site.
In steps 7 and 8, we downloaded and installed the Twilio Helper Library for PHP. This library is the heart of your Twilio-powered apps.
In step 9, we loaded our database schema into our database.
In step 10, we set up our config.ini
file.
In step 11, we set up our barebones index.php
file, which doesn't do much right now.
Finally, in step 12, we set up home.php
, which is the first page people will see when they load the site in their browser.
When you load the app now, you'll go to the index
file, which is specified by $app->get("/") route
.
All this page does is load the home.php
file found in the views
folder.
We've set up the basic framework for our PBX system. Now let's make it actually do something.