Experience

The Hidden Perils of Craft Plugin Service Classes

When writing a Craft plugin, you should be very selective about what you put in your service classes.

This advice appears to run contrary to the guidance given in the official Craft documentation, and keeping your plugin code in service classes is certainly preferable to keeping it in your controller of variable classes. But it’s still a bad idea.

Here’s why.

How and When to Use PSR-4 Autoloading in Your Craft Plugins

If you’re developing a reasonably sophisticated Craft plugin, chances are you’ll have a lot of class files which don’t really fit into the “standard” Craft plugin sub-directories of models, records, and so forth.

In this situation, you need a way to easily loading your “non-standard” classes as-and-when they’re required. PSR-4 autoloading is the perfect solution to this problem, and by the end of this article you’ll know everything you need in order to use it in your plugins.

Introducing Glossary for Craft

Today, we’re proud to release Glossary, a new Craft plugin.

Glossary makes it easy to implement a list of entries, grouped by X and ordered by Y. For example:

  • A list of staff members, grouped by the first letter of their surname, and ordered by forename;
  • A list of countries, grouped by continent, and ordered by population;
  • A list of recipes, grouped by cooking method, and ordered by cooking time.

It’s possible to achieve all of the above using Twig, but the result is messy and difficult to test.

Glossary keeps your templates neat, and has a suite of unit tests to ensure everything runs smoothly.

The Definitive Guide to Validation in Craft, Part 2: AttributeTypes

The first instalment of this series provided a high-level overview of validation in Craft. We covered what happens behind the scenes when Craft validates your model, which model validation methods are available (and what each one does), and how to define your model attributes.

This instalment is all about Craft’s AttributeTypes: what they are, what they do, and which ones are available.