Unlike most other frameworks that just combine a set of already existing tools, TwistPHP is wriiten from the ground up. Every class, module and template is written to work seamlessly with all the others.
Development is always ongoing and because it doesn't rely on anyone else's classes or code, updates, improvements and fixes can be deployed quickly and easily.
At the heart of TwistPHP is an MVC architecture which allows you to easily and beautifully organise your code. Gone are the days where you complicate your PHP and HTML. With TwistPHP, you can now throw away your PHTML files and separate your code out properly.
/app
folderThe app folder serves to keep all your project files separated from the framework. This makes it really easy to update TwistPHP without moving around a lot of files.
This is the place where all your site's models, views, controllers and resources live along with all the configuration files, cache files and logs. You can also add any hooks and overrides into the app folder which keeps all your project-specific code together.
TwistPHP allows you to have models which act as administrators for the content. Models are the classes that allow you to get, set and manipulate data from the database.
You can easily overwrite any of the built-in models to fully customise your projects without ever touching the core of the framework.
The views in TwistPHP are HTML-like .tpl
files. With a syntax inspired by CodeIgniter, view files can be included and nested within one another and use content provided by controllers to return data to the user.
To keep your PHP and HTML separate, but still reusable, views can also be PHP files to house logic like for loops and data manipulators.
TwistPHP provides you with several useful functions to use in template tags which require you to write less PHP. It also gives you the ability to write ternary operators to reduce PHP further.
The classes that get model data into views based on the request are the controllers. Controllers in TwistPHP allow you to respond individually to the four standard HTTP verbs (GET, POST, PUT and DELETE) as well as providing global responses for any request.
In addition to this, you can catch and pass variables from the URI into the controllers which allows you to make fully RESTful projects.
TwistPHP comes with a whole host of useful third-party resources such as jQuery, Modernizr, Font Awesome and Unsemantic as well as some custom tools like a JavaScript AJAX class that works perfectly with our AJAX controller.
The resources are really easy to include in your views as well. To include a copy of jQuery, simply use the tag {resource:jquery}
. You can use a second parameter of async
, defer
or inline
to include the resource in a number of different ways. Smilarly, you can use {js:...}
and {css:...}
to include JS and CSS files from your app folder.
Beneath the surface of TwistPHP lies a powerful and expandable routing engine.
Included are several base controllers you can extend, allowing you access to prebuilt user controllers for login pages, registration forms, forgotten password prompts as well as AJAX controllers which allow you to build a full AJAX server.
Routes can serve functions, views, controllers, perform redirects, add restrictions to certain URIs or allow a package to serve its routes.
<?php
$route = Twist::Route();
$route -> baseTemplate( '_base.tpl' );
$route -> get( '/test', function() { echo 'Hello world!'; } );
$route -> view( '/welcome', 'welcome.tpl' );
$route -> controller( '/%', 'MyProject' );
$route -> redirect( '/campaign', '/welcome' );
$route -> restrict( '/stats%', '/stats/login' );
$route -> package( '/cms', 'Packages\MyCMS\Routes\Backend' );
$route -> serve();
TwistPHP automatically optimises module loading for your specific purpose. When starting up, there are no instances of any models. As you start to use any functionality from the included models, instances are created and shared.
An instance of each shared model used is saved so it doesn't matter whether you use a model once or 100 times, there will only be a single instance.
This ensures that TwistPHP loads your project as quick as possible.
TwistPHP comes with its own database interactions — not too complex as to slow things down but powerful enough to cover nearly every query in your day-to-day code.
No more mysqli()
, fetch_assoc()
or mysql_real_escape_string()
. Interacting with your database has now just become a whole lot easier.
Object-oriented database records make creating and modifying data in your tables really easy. Create or get a record, set its values and commit it to the database. Easy as 1-2-3.
<?php
$people = Twist::Database() -> records( 'people' );
$fry = $people -> create();
$fry -> set( 'firstname', 'Philip' );
$fry -> set( 'surname', 'Fry' );
$fry -> set( 'dob', '1974-08-14' );
$fry -> commit();
For more complex SQL, you can use TwistPHP's query()
method which acts just like the sprintf()
function, but escapes all the variables before constructing the query.
<?php
$query = Twist::Database() -> query(
"INSERT INTO `people` (
`firstname`, `surname`, `dob`
) VALUES (
'%s', '%s', '%s'
)",
'Philip', 'Fry', '1974-08-14'
);
$query -> insertID();
To aid you with quick and dirty queries, TwistPHP comes with several database methods for selecting, searching for and counting records, all of which return you objects (or optionally just an array) with the methods you need to modify and save back to the database.
<?php
// SELECT * FROM `books`
$books = Twist::Database() -> records( 'books' ) -> get();
// SELECT * FROM `houses` WHERE 'number' = 16 LIMIT 1
$house = Twist::Database() -> records( 'houses' ) -> get( 16, 'number' );
// SELECT * FROM `cars` WHERE `make` = 'Ford'
$fords = Twist::Database() -> records( 'cars' ) -> find( 'Ford', 'make' );
Like any framework, major updates like functionality changes need to be done at a terminal. To make things easier for you, TwistPHP comes with a responsive web-based framework manager.
If it's updating your framework or .htaccess settings, clearing out your site's cache or enabling maintenance or development mode, the TwistPHP manager has you covered.
All the extra work that comes with login forms, authentication and sessions has already been taken care of. TwistPHP allows you to restrict areas of your project to certain user levels, create login and registration pages as well as handling basic things like the forgotten password functionality.
As part of the authentication system, TwistPHP provides you with a device manager which allows you to rename, remove access to and keep track of all your authenticated devices.
require( 'twist/framework.php' );