We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Accept
Improve your Craft CMS skills

How Do I Find The Craft CMS Library Files?

10 min read
Andy Golpys Made By Shape Founder

Struggling to locate those elusive Craft CMS library files? This guide will illuminate everything you need to know to easily find, view, and access the critical craft/app folder on local and remote environments. With handy troubleshooting tips and best practices for modifying libraries, you'll gain the knowledge to master Craft's powerful core codebase.

The craft/app folder contains the core Craft libraries. It's located in the root Craft install folder alongside config and templates. Access it locally via code editor or terminal. Remotely, use SFTP/SSH. Never edit files directly. Instead, override templates or extend with plugins to customize Craft safely.

Overview of Craft CMS

So, what is Craft CMS? Craft CMS is a flexible, user-friendly content management system (CMS) built on PHP and the Yii framework. It utilizes Twig for templating and has a thriving plugin ecosystem that allows for extensive customization. Craft provides an intuitive, customizable admin panel and frontend to manage and deliver content.

Some key features and benefits of Craft CMS include:

  • Streamlined content editing with a user-friendly interface. Craft provides a simple way for non-technical users to manage content.

  • Powerful templating with Twig. Craft leverages Twig, a mature templating language, for complete control over frontend markup and design.

  • Plugin ecosystem. Craft has hundreds of free and paid plugins that extend its functionality for things like e-commerce, SEO, social media integration and more.

  • Headless capabilities. Craft can be run as a headless CMS to deliver content via APIs to apps, sites and devices.

  • Flexible content models. Craft offers Matrix fields and other ways to create flexible, customized content structures.

  • Global content support. Craft provides native translation and localization features to manage multi-language sites and content. Has multi-site features.

Overall, Craft provides a customizable and extensible CMS platform for building sophisticated websites and applications, making it a popular choice with developers and content teams alike.

Craft CMS Architecture

Craft CMS utilizes the model-view-controller (MVC) software architecture pattern. This separates the application into three interconnected parts:

  • Models - This layer contains the business logic and data access code. Craft's models handle things like users, assets, entries and data storage/retrieval.

  • Views - The view layer handles presentation and output. In Craft, views consist of Twig templates that define the frontend markup and output.

  • Controllers - Controllers handle input and connect the models and views. They contain the logic that passes data to the Twig templates for rendering.

The MVC pattern provides separation of concerns, where each layer has a specific responsibility. This promotes modular code and enables easier testing and maintenance. For example, templating logic is separated from business logic.

Craft's implementation of MVC allows developers to hook into models, views and controllers to customize the core functionality of the CMS. Plugins can integrate deeply with Craft's architecture.

Craft File and Folder Structure

A Craft CMS project consists of a structured collection of folders and files. Some key items include:

  • craft - This folder contains Craft's core application code and config files. The craft/app folder houses models, controllers and services.

  • config - Important Craft and environment config files are stored here. This includes database credentials and general.php.

  • modules - Custom PHP modules and classes can be stored here.

  • templates - All Twig templates live here. This includes layouts, pages, emails etc. Templates are organized into folders by template type.

  • web - This is the web root and document root for the project. Frontend assets like CSS, JS and images are stored here. The index.php file lives here.

This file/folder structure separates concerns and organizes code and templates. For example, Twig templates are stored separately from application logic. Configuration is also stored independently. This structure enables easier debugging and organization.

Accessing the Craft CMS Library

Locating the craft/app Folder

The craft/app folder contains the core library files and dependencies for Craft CMS. This is where you'll find the main application code and classes that power the CMS.

By default, the craft/app folder is located in the root of a Craft CMS installation. So in a typical setup, it can be found at:

Copy code

/path/to/project/craft/app

This /craft folder is part of the default file structure of a Craft project. The craft folder sits alongside other top-level folders like /config, /modules, /templates and /web.

So when first accessing a new Craft codebase, navigating to the root project directory and looking for the /craft folder is the way to locate craft/app.

For example, on a local development environment, if your project was at a folder like:

Copy code

/Users/myuser/Sites/myproject

Then the full path to craft/app would be:

Copy code

/Users/myuser/Sites/myproject/craft/app

The craft/app folder can generally be expected to exist here on local, staging and production servers, assuming a typical project setup.

Viewing Library Files Locally

When working on Craft CMS locally, there are a couple ways to view and browse the library files inside craft/app:

  • Code Editor - Open the craft/app folder in a code editor like Visual Studio Code, Sublime Text etc. You can then navigate the folders and view files.

  • Terminal - On the command line, cd into the craft/app directory and use commands like ls (Linux/Mac) or dir (Windows) to list files. Open files with a command like cat filename.php or an editor.

  • IDEs - Craft developers often use IDEs (integrated development environments) like PHPStorm that include a file browser and other tools to navigate code.

  • Frontend DevTools - In the browser dev tools, the Sources panel can sometimes be used to browse static assets locally.

Some key things that can be accomplished when browsing the libraries locally:

  • Read through class files to understand Craft internals.

  • Find examples of APIs, utilities and conventions used.

  • Discover plugin integration points and extension techniques.

  • Copy examples into custom modules and plugins.

  • Debug issues by stepping through library code.

So exploring the craft/app libraries interactively can be very useful for learning Craft CMS development.

Accessing Libraries on Remote Servers

To access and browse craft/app on live production servers, remote access tools like SFTP, SSH or FTP are required:

  • SFTP - SFTP clients allow securely transferring and interacting with files over SSH. Popular tools like FileZilla can be used to connect to remote servers and browse their file system.

  • SSH - If command line access is available via SSH, you can use terminal commands like ls and cat to list and view files under craft/app.

  • FTP - Traditional FTP can also be used to connect and transfer library files from a remote host.

Some advantages of accessing the remote Craft libraries include:

  • Downloading copies of files to view locally.

  • Comparing against local versions to spot discrepancies.

  • Diagnosing issues on live sites that aren't reproducible locally.

  • Checking for any unauthorized modifications.

  • Backing up critical library files.

So having read-only access to craft/app on production is very helpful for maintenance, debugging and security.

However, libraries should never be edited directly on production servers. Only approved deployments and updates should modify these files. Direct modification can lead to errors and loss of changes.

Overall, the craft/app libraries contain the core logic and functionality of Craft CMS. Accessing them locally and remotely is key for development and troubleshooting.

Modifying and Extending the Library

Overrides and Extensions

When customizing Craft CMS, best practice is to avoid directly modifying the core library files in craft/app. Instead, safer alternatives like plugins and template overrides should be used where possible.

The risks of directly editing library files include:

  • Changes being overwritten on updates. Craft gets updated frequently, so local edits may be lost.

  • Potentially breaking functionality that depends on unchanged code.

  • Security issues if vulnerabilities are introduced.

  • Difficulty troubleshooting problems.

So for long-term maintenance, direct modification of craft/app is not recommended.

However, alternatives exist:

  • Plugins - Extend Craft's functionality by developing custom plugins. This safely hooks into Craft without hacking core files.

  • Template overrides - For front-end changes, override Twig templates from craft/app/templates/ in your own templates/ folder.

  • Modules - Additional PHP classes and modules can be added in /modules/ to extend Craft.

  • Config - The /config folder is designed to be edited for environment-specific configuration.

With these options, modifying the core library files becomes unnecessary in most cases.

Including Additional Libraries

To include third-party libraries not bundled with Craft, using a dependency manager like Composer is recommended:

  • Add packages to composer.json and run composer update

  • Composer will install packages under /vendor

Without Composer, libraries can be manually copied into the /vendor folder. But this misses out on versioning and autoloading benefits Composer provides.

No matter the method, only reputable libraries from trusted sources should be included, as they can pose security risks if compromised. Namespacing third-party classes is also good practice to avoid collisions.

Following Coding Standards

Craft has published coding standards and best practices for development.

When modifying or extending the core libraries, adhering to these standards is highly recommended:

  • Consistent conventions aid long-term maintenance.

  • Following established patterns makes code predictable.

  • Code style should match Craft's for consistency.

  • Edge cases and backwards-compatibility are handled.

Key aspects cover things like:

  • Code formatting, spacing and naming.

  • Class structure, properties and methods.

  • Handling dependencies and interfaces.

  • Writing reusable, testable code with few side-effects.

  • PHPDoc commenting for improved comprehension.

  • And more.

While not mandatory, abiding by Craft's standards makes professional-quality code that safely enhances the CMS. Documentation and official plugins can serve as references for best practices.

So for any custom libraries, modeling them after Craft's own high-quality coding will yield great results.

Troubleshooting and Maintenance

Identifying Missing Libraries

Sometimes critical files can go missing from the craft/app libraries, often due to unauthorized access or incomplete uploads. Some signs of missing libraries:

  • PHP errors about undefined classes, traits, functions.

  • Frontend or backend breaks entirely.

  • Major functionality stops working.

  • New updates or plugins fail to work correctly.

To help identify and diagnose missing files:

  • Examine error logs for clues about exactly what's missing.

  • Check file timestamps to see if craft/app was recently changed.

  • Diff against a known good Craft install to spot absent files.

  • Review server access logs to detect suspicious activity.

  • Confirm permissions are correct and files aren't incorrectly hidden.

With clues from these checks, the scope of missing libraries should become clearer to then address.

Re-uploading Libraries

If critical libraries have gone missing, the first step is restoring from a recent backup or version control if possible.

If no backup exists, uploading replacement library files is needed. Obtain an intact copy of craft/app from:

  • The same version install on another environment.

  • A fresh Craft archive (match versions exactly).

  • The Craft git repository tag for that version.


Then use SFTP/SSH file transfer to replace the missing craft/app contents. Double-check permissions are set properly after uploading.

For significant loss, a reinstall from scratch may be easier than manual file replacements.

Security and Updates

To keep libraries safe and up-to-date:

  • Restrict file access through SFTP and permissions. Disable FTP and filesystem Execution.

  • Apply all critical security patches and hotfixes for vulnerabilities.

  • Review changes and benefits before updating to newer Craft versions.

  • After updates, check craft/app for expected new/modified libraries.

  • Monitor logs for unauthorized access attempts.

  • Utilize version control to easily rollback bad changes.

  • Take regular backups in case recovery is needed.

Proactive library maintenance is key for security. Updates bring bug fixes, performance improvements and new features. But change logs should be reviewed before updating libraries on production.

Following security best practices will help prevent unexpected loss of files. And with proper recovery practices in place, repairing issues is straightforward.

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Share
Feedback
Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat