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

Improve your Craft CMS skills

An Easy Guide To Craft CMS And DDEV

10 min read
Craft CMS And DDEV

Craft CMS is a flexible, user-friendly content management system beloved by developers and content editors alike. But setting it up locally with DDEV for the first time can be daunting. This guide will walk through installing Craft on DDEV simply and painlessly. You'll learn the key concepts powering Craft's architecture, master creating and managing content, extend functionality with plugins, and develop locally with Git-based workflows. Everything you need to get up and running with Craft CMS and DDEV for your next project.

Craft CMS is a flexible content management system that is easy to set up locally using DDEV. The key concepts include Sections, Entries, and Fields for content modelling, Categories and Tags for organization, and powerful Twig templating. Plugins extend functionality, and multiple environments streamline development workflows. Overall, Craft CMS provides an intuitive yet customizable CMS for any project.

Installing and Setting up Craft CMS with DDEV

Software and Prerequisites

Before installing Craft CMS with DDEV, there are a few prerequisites that need to be in place first. The main ones are having PHP, Composer, MySQL and a code editor installed.

You'll need a PHP version greater than 7.0, preferably PHP 7.4 or 8.0. The easiest way to get this is to install PHP via a package manager like Homebrew on Mac or Chocolatey on Windows.

That will handle installing and configuring PHP for you.

Composer is the dependency manager for PHP. Download the Composer executable from getcomposer.org and install it somewhere in your PATH. Verify it's installed properly by running composer --version in your terminal.

For the database, MySQL 8.0 or MySQL 5.7 will work fine. MySQL also can be installed via a package manager like Homebrew or Chocolatey. Make sure the MySQL service is running.

In terms of a code editor, Craft CMS itself recommends VS Code, Sublime Text or PhpStorm. These all have great PHP support and integrations. VS Code in particular has some useful Craft CMS specific extensions.

Those are the key requirements. You'll also need git installed to clone repositories, and a terminal for running command line commands.

With those prerequisites in place, now we can move on to actually installing Craft CMS itself with DDEV.

Step-by-Step Installation Guide

Here is a step-by-step guide to installing Craft CMS on DDEV using Composer:

  1. Install DDEV on your local machine if you haven't already. Follow the installation instructions on the DDEV site.

  2. Open your terminal and cd to where you want the Craft project directory created. Run ddev config to configure a new DDEV project here.

  3. Once DDEV is configured, run ddev composer create-project craftcms/craft. This will use Composer to create a new Craft CMS project in the current directory.

  4. When Composer finishes installing Craft and its dependencies, commit the initial installation to git.

  5. Update composer.json to require the specific version of Craft you want. For example "craftcms/cms": "^4.2.0".

  6. Run ddev composer update to update Craft CMS to that locked version. Verify it worked with ddev describe.

  7. Copy .env.example to .env and configure your database credentials and other environment settings.

  8. Run ddev launch to open the Craft CMS web-based installer. Complete the installation process here.

  9. Once installation is complete, commit the Craft config changes to git. Your project is now fully installed!

  10. Run ddev launch to open the Craft control panel and explore the admin dashboard.

That covers the end-to-end process of installing Craft on DDEV with Composer. The key steps are configuring DDEV, using Composer to install Craft, updating the version, configuring environment settings, and running the web installer.

Troubleshooting Common Issues

There are a few common issues that may come up when installing Craft CMS on DDEV. Here are some troubleshooting tips:

  • Composer errors - If Composer is unable to install dependencies, try clearing its cache with composer clearcache and retrying. Make sure Composer is up-to-date and no PHP version conflicts exist.

  • Database connection failures - Double check your database credentials in .env. Try manually connecting to the database first to verify it works. Restarting DDEV can also resolve some database issues.

  • Permissions problems - Craft CMS may throw permissions errors if it can't write to things like /storage. Run ddev filepermissions to ensure proper write permissions exist.

  • Web installer not loading - Make sure your .env settings are correct, run ddev restart, and try manually navigating to /admin/install on the project URL.

  • Existing project issues - For existing projects, be sure to commit your code first before re-installing Craft or switching PHP versions with DDEV.

  • Browser console errors - Check the browser console for errors. JS errors can prevent the installer from loading correctly.

Pay attention to error messages and stack traces. Search online for the specific error or issue if needed. The DDEV and Craft CMS docs also have lots of useful troubleshooting tips. Don't hesitate to reach out to their support channels for help.

Creating Your First Craft CMS Project

Project Setup and Configuration

When embarking on creating your first Craft CMS project, one of the most important steps is spending time upfront to properly set up and configure the project architecture and environment. This will pay major dividends as the project grows and scales down the road. First, come up with a logical project name to use consistently across directories, databases, domains, etc. For example "my-awesome-project". Next, dive into the main configuration files like general.php and app.php to define foundational elements like the site name, language, timezone, environments, and security keys. Setting up robust dev, staging, and production environments is crucial for smooth deployment down the road.

Install any must-have plugins as well during the initial setup, like SEOmatic plugin for SEO optimization or Redactor for wysiwyg editing. Setting up a base _layout.twig template is another good idea, to hold all the core markup of the project that will be inherited across templates. Don't forget to create a .gitignore file too, to exclude files like storage/, vendor/, and sensitive configuration files from source control.

Overall, investing time in thoughtful project configuration, establishing environments, installing key Craft CMS plugins, and laying out the initial file structure will pay dividends for the lifespan of your Craft CMS project. It sets you up for scalable growth down the road.

Folder Structure and Organization

When organizing the folder structure of your Craft CMS project, you'll want to leverage the flexibility of the system while keeping things logically separated into core folders. Here is a typical structure:

The config/ folder holds all core configuration files like general.php, app.php, db.php, routes.php and more. The modules/ folder contains any custom modules and business logic across the project. The plugins/ folder stores both 1st and 3rd party plugins. The public/ folder holds the index.php site entry point along with any web accessible assets. The storage/ folder contains temporary files like logs, cache, compiled templates and more. The templates/ folder stores Twig templates organized into subfolders by type like pages/, emails/, layouts/ etc. The tests/ folder holds unit and integration tests for custom modules. Finally, the vendor/ folder contains Composer dependencies like the Craft CMS core.

This structure promotes separation of concerns between modules, templates, and plugins while keeping config files together. The public/ and storage/ folders are excluded from version control as they contain temporary files. Overall, it enables a scalable and organized architecture. Don't be afraid to customize it as needed per project.

Launching Your Project Locally

Once your Craft CMS project is configured and the files are organized, it's time to launch it locally for development. This is where DDEV comes in very handy for quickly spinning up a local environment.

First, tell DDEV where your site's entry point lives by running "ddev config --docroot=web" in terminal. This lets DDEV know that web/index.php is the front controller.

Next, start the environment with "ddev start". DDEV will now build the containers and provide a default URL like project-name.ddev.site.

With the project started, simply navigate to this URL in your browser to view the frontend site. Access the admin control panel at url.ddev.site/admin and log in with the credentials created during installation.

The control panel is where you can now start populating content, tweaking templates, and integrating plugins. DDEV provides a completely self-contained local environment so you don't have to fuss with virtual hosts. Just focus on building the project!

Some key DDEV commands include "ddev launch" to open the site URL, "ddev ssh" for shell access, "ddev logs" to view logs, and "ddev stop" to shut down the environment when done.

In summary, carefully configuring, organizing, and launching a Craft CMS project sets a strong foundation. DDEV streamlines the process of scaffolding a local environment to start building in. With these fundamentals in place, you're ready to start crafting a stellar digital experience.

Key Concepts and Architecture of Craft CMS

Sections, Entries and Fields

Craft CMS provides a flexible and modular content architecture using Sections, Entries, and Fields.

Sections define different content types and structures. For example, a "News" section for news posts, or a "Products" section for product catalogue entries.

Entries are the actual content items created within these sections. So each news story would be an entry in the "News" section.

Fields then make up the content within entries. Field types like "Title", "Body", "Image" etc allow you to customize the structure of entries in each section.

With this Sections, Entries and Fields model, Craft allows fully customizable content modelling. You can create any number of sections with different field layouts to structure content as needed.

Sections and entries provide the overall content entities, while fields enable detailed content composition. This modular separation provides enormous flexibility for modelling diverse content structures.

Categories, Tags and Users

In addition to Sections, Entries and Fields for content, Craft CMS provides other helpful models for content organization, association and user management.

Categories allow you to classify and organize entries. For example, assigning news entries to categories like "Business", "Technology", "Sports" etc. Categories have their own structures with nested sub-categories if desired.

Tags enable you to associate relevant keywords and metadata with entries. News entries could have tags like "Finance", "SaaS", "Ecommerce" etc. Tags are flat, unlike hierarchical categories.

Users represent the people who access and manage content in the CMS. Fields like name, email, password and permissions enable detailed user management.

These models complement Sections, Entries and Fields to provide well-rounded content modelling capabilities.

Templating, Routing and Plugins

Craft leverages the powerful Twig templating language for front-end presentation. Template inheritance and blocks provide DRY, maintainable markup.

The flexible routing system enables routes for both front and backend using simple YAML files. No need for complex regular expressions.

Plugins extend Craft's functionality with custom fields, widgets, integrations and more. They can add light shopping carts, newsletters, social media and countless other features.

Together, the templating, routing and plugin systems enable extensive customization and flexibility on both the presentational and functional layers of a project.

In summary, Sections, Entries, Fields provide customizable content modelling. Categories, Tags and Users enable multifaceted content and user management. And Twig, routing, and plugins facilitate full front-end and back-end customization.

These composable architectual concepts make Craft CMS incredibly versatile for all types of digital projects, informing both the content structure and presentation.

Managing Content in Craft CMS

Creating Entries and Using Fields

In Craft CMS, creating and populating content is handled through Entries and Fields.

To create a new entry, navigate to the desired section in the Craft control panel, like "News" or "Products". Click the "New entry" button to create a blank entry.

The entry edit screen will now load, containing any Fields you've defined for that section - things like Title, Body, Image, etc. Populate these fields with your content as needed.

Use the Title field for the entry title. Body for the main text content. Image for any associated image. Craft provides many field types like Dropdowns, Checkboxes, Table fields and more to capture all kinds of content.

When you're done populating the fields, hit "Save" and the new entry is published. You can create as many entries as needed in a section to build up your content repository.

The Fields assigned to a section give you flexibility in modelling content structure. Add, remove or re-arrange fields at any time as needed.

Overall, Entries represent the actual content items, while Fields enable shaping the content within the entries. This makes adding and managing content intuitive.

Organizing with Categories and Tags

Categories and Tags help organize and structure content for easy discovery and management.

You can create a hierarchy of Categories within Craft, then assign entries to one or more categories. For example, assigning a "Business" and "Finance" category to a news article.

Tags work similarly, enabling you to tag entries with relevant keywords like "SaaS" or "Ecommerce". But Tags are flat versus hierarchical like categories.

Categories and tags appear on entry pages, helping site visitors filter and navigate content. And they provide an organizational system for the content editors.

Using Categories and Tags to thoughtfully classify and keyword your content makes entries easy to manage, find and consume.

Managing Assets and Users

Craft provides a few other important content management capabilities as well - digital asset management and user management.

All digital files like images, PDFs and videos can be uploaded to structured Asset Volumes in Craft. Then these assets can be linked to entries or displayed directly.

User accounts, roles and permissions enable managing who accesses the CMS and what they can do. Set up an admin user, editor users, translate-only users etc.

So in summary, Entries and Fields facilitate creating the actual content. Categories, Tags, asset volumes and users help manage, organize and structure that content.

By leveraging these robust content modelling concepts, Craft CMS provides a flexible yet powerful content management experience.

Extending Craft CMS with Plugins

Installing and Configuring Plugins

One of the most powerful features of Craft CMS is its plugin ecosystem. Plugins allow for extending Craft's functionality by integrating new features and systems.

Plugins can be discovered and installed directly within the Craft CP via the plugin store. Click "Settings" then "Plugins" to browse available plugins.

Find the one you want and click "Install". If purchased, enter your license key, otherwise install the free trial. Craft will download and install the plugin, then add the plugin to Craft CMS.

Alternatively, plugins can be installed via Composer by requiring them in your project's composer.json. Run "composer require vendor/plugin" to add and install a plugin.

Once installed, navigate to the plugin's settings page to configure it. Options may include things like API keys, layouts, configurations and more. Enable elements as needed.

Things like widgets, fields, integrations and more will now be available across your templates and the control panel.

Overall the built-in plugin store combined with Composer makes finding, installing, updating and configuring plugins a breeze.

Essential Plugins for Craft CMS

Some plugins that provide tremendous value out of the box include:

  • SEOmatic - Enables optimizing pages for search engines via customizable titles, meta tags, social tags, JSON-LD and more.

  • Redactor - Adds a robust WYSIWYG editor for formatting rich text content in entries and pages.

  • Contact Form - Allows easily building contact forms with spam-protection and notifications.

  • Social - Integrates and displays social media feeds from Twitter, Instagram etc.

  • Sprout Forms - Creates front-end forms and data collection across your site.

  • Commerce - Provides ecommerce functionality like products, carts, and payments.

  • FeedMe - Imports content from external sources via RSS feeds and APIs.

These plugins deliver essential features and integrations that most Craft sites need right out of the box. They extend Craft's core capabilities significantly.

Developing Custom Plugins

For more advanced customizations, developers can create custom plugins for Craft.

This involves registering controllers, services, variables and more. Your plugin code goes into a src/ directory that gets packaged into a zip plugin.

The plugin would provide its own CP section for managing custom elements and settings. Front-end code implements functionality across templates.

All of Craft's APIs are available for doing things like querying content, integrating APIs, creating custom fields and more.

Developing a custom plugin enables tailoring Craft CMS into a custom CMS uniquely suited to your project needs.

In summary, plugins allow infinitely extending Craft's core by integrating new systems or developing custom solutions - a key aspect of Craft's power and flexibility.

Templating and Theming in Craft CMS

Twig Templating Syntax

Craft CMS leverages Twig for its powerful templating capabilities. Twig syntax provides many useful features for building templates.

Variables like {{ entry.title }} and {{ entry.myField }} output content dynamically from entries.

Filters like {{ entry.postDate|date('F jS, Y') }} format values.

Tags like {% for entry in entries %} iterate over arrays.

Functions like {{ source() }} and {{ redirect() }} link assets or redirect pages.

Operators like {% if entry.myField == 'Foo' %} allow logic and control flow.

Twig makes it fast and easy to output content, loop through related entries, apply formatting, display images/PDFs, and more thanks to its myriad of capabilities.

Developers with PHP experience will find Twig very intuitive for building front-end templates in Craft CMS.

Global vs Entry Templates

Craft CMS provides two main types of templates - global templates and entry/section templates.

Global templates define reusable content blocks like headers, footers, sidebars etc. that are shared across all pages via {% include %}.

Entry and section templates specify the unique layout and content for individual entries and sections. This is where you output the {{ entry }} content.

For example, the blog post index page would be a global blog template including each post entry using {% for entry in entries %}. The blog post entry template defines the markup for each individual post.

Page templates define one-off pages. Section templates define common layouts for all entries in a given section.

Mixing global and entry templates allows creating a flexible DRY site architecture in Craft CMS.

Theming and Custom Styling

Building a custom theme in Craft involves customizing the front-end HTML, CSS and Javascript.

HTML structure lives within Twig templates. Override templates from the default _layout.twig base template to modify markup.

For styling, create a /css/ folder with .css files for any custom CSS. Import these using {% do view.registerCssFile() %} in templates.

For Javascript, add custom JS files to /js/ and import them with {% do view.registerJsFile() %}.

Within these files, use common CSS and JS to apply styling overrides, effects, interactions etc.

Craft CMS provides complete front-end flexibility. Tweak templates for layout and markup changes, CSS for styling and visuals, and JS for interactions and effects.

With some HTML, CSS and JS skills, you can craft a custom theme to tailor the exact look, feel and functionality you want.

In summary, Twig templating enables powerful data output, global templates allow reusable markup, and CSS/JS customization provides total front-end theming flexibility in Craft CMS.

Environments and Deployments

Local, Staging and Live Environments

For a scalable Craft CMS project, it's important to properly setup and manage local, staging, and live environments.

The local environment runs on your own machine for development. Using MAMP, Vagrant, Docker etc. The database and assets are local.

Staging mirrors the production environment for testing prior to launch. It uses the same database and assets as production.

Live is the public-facing production environment accessed by end users. Production database, assets, and DNS.

Configuring these environments enables safely developing locally, testing changes on a staging server, then deploying to live when ready.

The Craft control panel makes managing environments easy by defining environment-specific settings like system name, language, security keys etc under Settings → Environments.

Variables like {% if craft.config.environment == 'staging' %} in templates allow environment-specific logic.

Overall, properly configuring and leveraging multiple environments is crucial for an optimized Craft CMS project workflow.

Migrating from Local to Live

When ready to launch your Craft CMS project, you need to migrate or deploy the codebase from local to live production hosting.

For the codebase, commit all templates, plugins, modules etc into Git. Push this code to the production server, pull it down, and run Composer to install dependencies.

For the database, take a database dump locally and import it into the production database. Update any credentials in general.php.

Assets need to be manually transferred over as well. Copy all your local asset files onto production. Sync any asset volumes to point there.

Double check configuration like security keys, environment variables, asset CDN settings etc.

Enable maintenance mode during the cutover to avoid any access errors or confusion.

Following best practices for deploying code, data and assets helps ensure a smooth transition from local to live.

Managing Configuration Differences

Since local, staging and production are distinct environments, some key configurations will vary:

  • Database credentials in db.php

  • Asset volume locations

  • Environment variables

  • Security keys in general.php

  • Front-end URLs

Using environment variables in .env files helps manage these differences. For example, .env.production stores production config.

The Craft config system makes accessing environment-specific configs easy.

Migrate script can also automate propagating config changes across environments.

Overall, thoughtful management of configuration differences across local, staging and production is vital for a stable Craft CMS project architecture.

In summary, leveraging multiple environments and deploying carefully between them is crucial for any scalable, real-world Craft CMS project.

Debugging and Troubleshooting

Dev Mode, Debugging, and Logs

When issues arise on a Craft CMS site, enabling Dev Mode along with debugging tools and logs provides helpful troubleshooting insights.

Dev Mode can be enabled under Settings → General in the control panel. This shows more verbose errors and profiling info.

Craft provides native debugging tools as well, accessible via templates. {{ dump() }} dumps variables. {{ craft.app.debugger.mailPanel }} shows emails.

Log files record errors, requests and more that aid debugging. Access logs under Admin → Utilities → Logs or directly on the filesystem under storage/logs/.

Plugin logs can also contain clues. Check their documentation for logging config.

For JavaScript issues, browser DevTools are invaluable for inspecting network requests, console errors, elements, and debugging.

Enabling Dev Mode provides a troubleshooting-friendly Craft environment. Combine it with debugging utilities and log analysis for identifying any PHP, Twig or front-end issues.

Common Errors and Solutions

Some common errors that may appear in Craft include:

  • HTTP errors like 404s and 500s. Check logs for failed controllers.

  • Twig errors from incorrect syntax or undefined variables. Enable Twig debugging.

  • Database query failures. Ensure database credentials are correct.

  • Plugin conflicts due to incompatible versions. Try updating plugins.

  • Asset transform issues from misconfiguration. Double check image transform settings.

  • Memory exhaustion. Increase PHP memory limit under Config → General.

Many errors provide specific traces to dig into like the file, line number and stack trace. Search Craft's GitHub issues for similar reports.

Restarting services, clearing caches, restoring databases, and re-uploading assets can also help resolve problems.

Overall, carefully inspecting error specifics along with intelligently debugging and troubleshooting will overcome many common Craft issues.

Help Documentation and Resources

Craft CMS provides excellent official documentation with troubleshooting guidance:

For complex cases, Craft CMS support engineers can provide personalized help troubleshooting and debugging: https://craftcms.com/support

Leveraging these rich resources helps debug even the trickiest issues that may arise in Craft CMS projects.

In summary, enabling Dev Mode, inspecting logs, checking Stack Exchange, and contacting support empowers troubleshooting and resolving problems.

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. 

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