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?
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.
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 represent the structure of "objects" that will be used by the site. An example might be a
Image, or a
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.
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:
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 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.
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.
is_between(value, min, max)
A quick way to convert a time in the past into a text with units. Ex: "ten hours".
Sanitizes text to be displayed in a browser.
Sanitizes text to be displayed in a browser but preserves newlines as
Print a string with a newline at the end.
Prints a string, prefixed with a timestamp and suffixed with a newline. (Useful in command-line scripts).
Lattice has some further syntactic sugar in the form of macros for common patterns.
If a single backslash is followed by the characters
[a-zA-Z0-9]+ then Lattice will attempt to find a matching class in the
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
Returns a string with the time formatted for a MySQL datetime value.
Renders a 403 and stops execution if
\app\model\user::current()->is_logged_in evaluates falsey.
Eases debugging by placing a
Tutorials & Demos