FrostMVC provides a simple and easy-to-use structure for building web applications, with beginners in mind. It is designed to be simple to understand, yet flexible and modular, targeting developers new to the MVC structure. It comes with MySQL database access through the Model class and uses Twig for views. FrostMVC is not open-source, but it is free to use for personal and commercial web applications.
FrostMVC needs the following to be installed before you run it:
mod_rewrite enabled (FrostMVC's .htaccess sends every URL to index.php)You can install PHP, Apache and MySQL at once with one of these packages:
To add third-party PHP libraries, install Composer.
FrostMVC folder to your web server's document root:
C:/wamp64/www (or C:/wamp/www)C:/xampp/htdocs/Applications/MAMP/htdocs, or the document root set in MAMP's preferenceslibraries folder and run composer install. Skip this if the download already includes libraries/vendor.$config['url'] in app/settings.php to match. A virtual host (e.g. http://myproject.test) is recommended.
All settings are in app/settings.php. It fills a $config array, creates the site from it, and then sets up sessions, CSRF protection and the database.
$config['url'] = Site::getProtocol() . '://' . Form::fis('HTTP_HOST') . (strpos(Form::fis('HTTP_HOST'), 'localhost') !== false ? '/FrostMVC' : '');
$config['name'] = "My Website";
$config['email'] = "hello@mywebsite.com";
$config['namespace'] = 'MyApp';
$config['environment'] = "Development";
$config['timezone'] = "Asia/Manila";
url | Your site's address, without a trailing slash. It becomes BASEPATH (and RESOURCEPATH for assets/). A wrong value gives 404 errors. The starter detects the host, adding the folder name on localhost. |
name, email, contact, mobile, address, facebook, FBID, twitter | Site details, read back with getName(), getEmail()… in PHP, or {{ site.getName() }} in Twig. |
namespace | The PHP namespace of your app. Controllers use MyApp\controllers, models MyApp\models, libraries MyApp\libraries and routes MyApp\routes; FrostMVC autoloads them. |
environment | 'Development' shows detailed errors, enables /phpinfo and Twig's dump(). 'Production' hides them. Always use 'Production' on a live site. |
timezone | A PHP timezone name, e.g. 'Asia/Manila' or 'UTC'. |
List the helpers (libraries/helpers/*.php) and library classes (libraries/*.php) to load on every request:
$config['libraries'] = [
['helper', 'generalHelper'],
['library', 'TwigExtensions'],
];
Load others only where you need them, from a controller: $this->require->helper('myhelpers') or $this->require->library('Mailer', $this).
$instance = Site::addInstance($config);
This applies the configuration. Afterwards the site is available as $instance in settings.php, as getInstance() or Site::$instance anywhere else,
and as site in Twig. See the Site reference.
Add variables that every view can use:
$instance->twig->addGlobal('version', '1.0.0');
$instance->twig->addGlobal('nonce', sha1(Date_Time::Now() . rand()));
In a view, {{ version }} prints 1.0.0. See Views & Twig for the variables FrostMVC provides.
Logs::setDirectory('assets/tmp/logs');
Session::setOption('cookie_secure', $config['environment'] === 'Production' ? 1 : 0);
Session::setOption('cookie_samesite', '');
Session::memcached('127.0.0.1');
Logs::setDirectory() — where Logs::write() and query errors are logged.Session::setOption() — PHP session cookie options. Keep cookie_secure on in Production (HTTPS only).Session::memcached() — stores sessions in Memcached at that address. Without the Memcached extension or server, FrostMVC falls back to regular PHP sessions.Every POST request must carry a valid CSRF token; forms opened with Form::start() include it automatically.
Security::$CSRFTokenConfig = [
"regenerate" => false,
"token-name" => "x-csrf-token",
"exclude" => []
];
regenerate — issue a new token after each check. Turn it on for sensitive forms such as login and registration, so a token cannot be reused.token-name — the name of the token field and cookie.exclude — URL paths (full or partial) that skip the check, e.g. webhooks that are called by other servers.FrostMVC supports MySQL and MariaDB.
$dbname = "mydatabase";
$servername = "localhost";
$username = "root";
$password = "mypassword";
define("TABLE_PREFIX", "");
define("TABLE_SUFFIX", "_tbl");
DB::$db = new DB($servername, $username, $password, "$dbname");
TABLE_PREFIX and TABLE_SUFFIX are added to model names to find their tables: with the settings above, the Accounts model uses accounts_tbl.
Set both to '' if your tables have no prefix or suffix. See Models & Database.
Call your libraries' setup methods at the end of settings.php, after everything else is configured. The starter registers its Twig functions and filters this way:
(new TwigExtensions)->load();
Learn how the pieces fit together, then look up any class or method in the API reference.
Read the Documentation API Reference