Lattice: Flexible PHP 7 Framework

How do you reconcile developing in PHP once you've been exposed to the sweet syntactic sugar of Ruby?

And after you've been working in Scala and Rust, how do you pretend like returning an Option isn't better than crossing your fingers and hoping for the best?

Or after using functional JavaScript, how do you pretend like method chaining isn't useful, and let's be honest, kinda fun.

One option: you make a little framework to bridge the gap.

Lattice is a modern (PHP 7+) framework that encourages developers to use the MVC pattern and provides syntactic sugar to make development easier.

Quick start

Lattice supports general MVC-based PHP projects. You can start using it by grabbing the files from github. If you want to start building a website then consider using Launchpad on Lattice.

Launchpad is built on top of Lattice and adds models, views, and controllers to get web projects online faster. Specifically, it adds models and controllers to handle users, sessions, logins, registration, and file (& image) uploading. It also adds a default page template and default css styles.

The MVC Pattern

Lattice aims to provide a useful framework, but not to reinvent the wheel. It is built upon the commonly understood MVC pattern.

Models

Models represent the structure of "objects" that will be used by the site. An example might be a User, an Image, or a BlogPost. Lattice provides an abstract Model class which is a base for all the models you might use. This basic model adds some structure and offers methods to set and retrieve data.

The framework allows models to be extended with traits to add more functionality. One trait, Saveable, is provided to add methods to save to a database. You can add more traits for your needs.

Views

The views allow us to display our project. We can render individual models or whole web pages. HTML (or JSON) plus PHP can be written in .tpl.php files and rendered by calling:

render(filename, data);

Templates can be nested to build complex and dynamic pages. You can also use render_to_string to render the template into a variable.

Controllers

Controllers encapsulate the behavior of the site. This can be passive actions like showing a list of articles, or active interactions such as registering a new account.

The actions in a controller can return Attempt objects. These represent if the action was successful or a failure, and allow you to respond accordingly.

Syntactic Sugar

Inspired by constructs in other languages, Lattice includes a handful of functions to make writing PHP safer and less repetitive.

Object and Array access

get_or_else(object, key, [default_value])

This method makes it easier to safely access elements in an object or an array without erroring if that key is missing. You can define a default to return if nothing is found, otherwise null will be the default.

comparison

is_between(value, min, max)

Date-time formatting

human_time_ago(date)

A quick way to convert a time in the past into a text with units. Ex: "ten hours".

Text formatting

clean(string)

Sanitizes text to be displayed in a browser.

clean_br(string)

Sanitizes text to be displayed in a browser but preserves newlines as <br /> elements.

println(string)

Print a string with a newline at the end.

printlog(string)

Prints a string, prefixed with a timestamp and suffixed with a newline. (Useful in command-line scripts).

Macros

Lattice has some further syntactic sugar in the form of macros for common patterns.

Model Shorthand

\foo

Macro for \app\model\foo. If a single backslash is followed by the characters [a-zA-Z0-9]+ then Lattice will attempt to find a matching class in the /app/model/ directory.

Controller Shorthand

\foo_controller

Macro for \app\controller\foo. Similar to the model macro, if a single backslash is followed by ([a-zA-Z0-9]+)_controller then Lattice will attempt to find a matching class in the /app/controller/ directory.

Rendering templates

render(filename, data)

Shortcut for \app\template::render(filename, data).

render_to_string(filename, data)

Shortcut for \app\template::render_to_string(filename, data).

Database queries

sql_find(prepared_query, params)

Shortcut for \app\database::sql_find(prepared_query, params).

sql_set(prepared_query, params)

Shortcut for \app\database::sql_set(prepared_query, params).

sql(prepared_query, params)

Shortcut for \app\database::sql(prepared_query, params).

sql_date([time])

Returns a string with the time formatted for a MySQL datetime value.

Access control

login_enforcement_check()

Renders a 403 and stops execution if \app\model\user::current()->is_logged_in evaluates falsey.

Debugging

inspect(object)

Eases debugging by placing a var_dump(object) inside of <pre> tags.

The Code

Lattice

The code for Lattice can be found on GitHub:

Lattice Launchpad

You can also get Lattice with demo pages, simple CSS, and JS syntactic sugar in Launchpad:

Published: August 11, 2018

Categories: webdev, infrastructure