Application File Structure

    • controllers
    • extensions
    • libraries
    • models
    • resources
    • tests
    • webroot

    Let’s tackle each of these folders one-by-one, covering what each is used for, and how you can best organize your application.

    The config folder contains three main pieces: bootstrap files, your connections information, and your routes definitions.

    The main bootstrap file in the config folder is bootstrap.php. This file is second file PHP will execute in any request cycle, and it loads up the li3 framework along with any other extras that you define there. As such, it’s a great place to place initial application configuration.

    The best practice for bootstrapping parts of your application is by writing specific bootstrap files and placing them in config/bootstrap/filename.php. Once written, include the configuration into the main bootstrap file:

    Connections

    The connections.php file in config lists the connections you have to external data resources, most often some type of database.

    Connections should be listed in this file, as calls to Connections::add(), like so:

    The particulars on the adapter will shape how the connection definition is put together, but this list should constitute what you’ve got in connections.php.

    Routes

    Routes definitions is how you inform the framework how URLs and bits of code match up. At the most basic level, it’s how you tell li3 which controller should respond to a request to a given URL.

    For example, I could specify that any request to /login is handled by UsersController::login() like this:

    More on routes later, but this file should house all such configuration information.

    Controllers

    Application controllers are housed here. Each controller is named in CamelCased, plural format (i.e. PostsController.php), and placed here.

    Extensions

    The extensions folder is meant to store extension classes that you’ve created for your application. Custom helpers, adapters, console commands and data classes.

    Adapter

    Custom application adapters should be placed in this folder, organized by subfolder. For example, if I created a custom adapter for the class, based on my LDAP setup, I’d create the implementation in /extensions/adapter/auth/Ldap.php.

    Custom console applications are placed in command. Commands you create should be built similar to those you see in /lithium/console/command.

    As an example, say I created a custom mailer queue, and I need to create a command to fill up the queue with announcement emails and trigger the send process. I’d create a new class called QueueFiller that extends lithium\console\Command, and place it in app/extensions/command/QueueFiller.php.

    Once there, running li3 queue-filler from the command line will trigger the logic I’ve written.

    Data

    Occasionally a new application will require custom data classes to work with a storage engine not already accommodated for by core classes.

    If you’ve got some proprietary database, and it’s result objects or query structures are very customized, you’ll extend the classes in the lithium\data namespace (i.e. Source, source\Database or source\Http) and place them here for use in your application.

    Helper

    Custom view helpers are common need. These classes should extend lithium\template\Helper and be placed in this folder.

    The libraries folder is meant to house entire li3 applications—or plugins—that your main application makes use of.

    Third-party, non-li3 libraries should be placed here as well. If you’re integrating with Facebook, or using part of the Zend Framework in your application, place those files here as well.

    Models

    Application models are stored here, in CamelCased, plural naming format (i.e. Posts.php).

    Resources

    The resources folder is used by a li3 application as a non-web viewable storage place for application data. By default globalization files (such as .po string files) and temporary application files are stored there.

    As you build your application, the resources folder is a great place for other application data like custom cache files, SQLite databases, or a temporary spot for uploaded files.

    The tests folder is where the unit testing framework files for your application reside. The testing setup for li3 is covered elsewhere, but let’s cover what ends up in each of the testing subfolders here.

    Cases

    All of the actual unit testing logic for the main MVC classes in the application go in the folder. Subfolders should already exist inside of cases to prompt you where each sort of test case should go.

    Unit tests in the integration folder test the interaction between two or more classes, or abstract class implementations.

    Mocks

    Classes in mocks are used to facilitate certain unit tests. For example, if I’m testing a custom model I’ve created, I can create a new class in mocks/data/MockSource.php and use it in my model unit testing logic.

    It is recommend that you use subfolder organization that mimics your application’s and li3’s namespacing and folder structure.

    Views

    The views folder contains any pieces of view-level code in your application. The two main core folders here to note are elements and layouts. Other view folders here contain views organized by controller name.

    Elements

    Elements are small bits of view code that are used in multiple views. A common navigational element, menu, or form might end up as an element in this folder. Element files are suffixed with a compound extension: the first showing the type of view code, and the last as .php.

    Example element filenames might include nav.html.php, animateLink.js.php, or header.xml.php.

    Layouts

    Views (and the elements they may contain) are rendered inside of view layouts. The layouts for your application reside in this folder, named similar to elements.

    A layout named default will be used to render a view if none other has been specified by the controller.

    Along with the elements and layouts folders, many other controller-specific view folders will reside here.

    One default example is the pages folder here that holds all the views for the core PagesController. As you create new controllers and views for your application, create new folders here, named after your controllers.

    Webroot

    Things like JavaScript files, images, Flash objects, and movies should end up here for easy access for your web server to deliver to the client.