PSR-4 Autoloader : custom composer.json - tutorial #1

Silverstripe Version:
4.1.1
Question:

Going through tutorial #1

This composer.json is based on the lessons and goes in the app/composer.json. If you are creating your own theme what do you put in place of name, description, licence, etc.?

This autoloading function, if I don’t know the name of namespaces what do I do with this? When does it become important? Is it important?

{
    "name": "silverstripe/lessons",
    "description": "The silverstripe.org lessons example code",
    "license": "BSD-3-Clause",
    "authors": [
        {
            "name": "Uncle Cheese",
            "email": "aaron@silverstripe.com"
        }
    ],
    "require": {
        "silverstripe/recipe-cms": "^1.1"
    },
    "autoload": {
        "psr-4": {
            "SilverStripe\\Lessons\\": "src/"
        }
    }
}

You can choose any name, it’s not important unless you’re publishing it as an open source module. If you are doing that, it will be the package name that developers use to install with Composer via Packagist (assuming you submit it to packagist).

Description is up to you as well.

The license is important - if it’s an open source project you should choose an appropriate license for what you want. SilverStripe uses BSD-3-Clause, MIT license is also very common. See https://choosealicense.com/ for more help.

The PSR-4 autoloader is not crucial in SilverStripe 4.x since it still uses the SilverStripe class manifest to load all PHP classes and refer to them, so you don’t necessarily need it. It is however a good idea to add it now to future proof, and it’s possible/likely that it will become a requirement in SilverStripe 5.

Essentially the autoloader definitions you define should point to the PHP classes that exist in your project (mysite in SS 4.0 and 4.1 and app in 4.2+). What you choose is up to you, but it’s common to match them to the package name (see “name” in composer.json).

More information: PSR-4: Autoloader - PHP-FIG

Hope this helps!

Alright. Thank you.

It’s pretty awesome making a site from scratch; totally my first go. I’m hoping to get this css grid working on my new theme, and then share it with the community. Perhaps that can become a theme for others.