### Create a New Project with Minimal Installation
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Perform a lightweight installation with only the essential components using the --prefer-dist option.
```bash
composer create-project silverstripe/installer my-project --prefer-dist
```
--------------------------------
### Install Dependencies and Lint Documentation
Source: https://docs.silverstripe.org/en/5/contributing/documentation
Run these commands from the command line to install dependencies and lint the documentation locally. Ensure Composer and Yarn are installed first.
```bash
yarn install
composer install
yarn lint
```
--------------------------------
### Install and Use Node.js Version
Source: https://docs.silverstripe.org/en/5/contributing/build_tooling
Installs and uses the recommended Node.js version using NVM. Ensure Node.js is installed before proceeding with Yarn.
```bash
nvm install && nvm use
```
--------------------------------
### Check Composer Installation
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Verify that Composer is installed and accessible on your system by running the help command.
```bash
composer help
```
--------------------------------
### Create Project with Latest Unstable Silverstripe Installer
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Use this command to start a new Composer project based on the latest unstable version of silverstripe/installer. This is useful for contributing to Silverstripe or accessing early fixes.
```bash
composer create-project silverstripe/installer ./my-project 5.x-dev
```
--------------------------------
### Start Daemon Process via Sake
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Manage a daemon process using `sake` by starting it with the `-start` flag followed by the process name.
```bash
sake -start my_process
```
--------------------------------
### Install Silverstripe CMS via Composer
Source: https://docs.silverstripe.org/en/5/getting_started
Use this command to create a new Silverstripe project. Ensure Composer is installed and accessible in your environment.
```bash
composer create-project silverstripe/installer my-project
```
--------------------------------
### Install Sake Wrapper
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Copy the `sake` executable to `/usr/bin/` for easier access. This command is for UNIX-like systems only.
```bash
cd your-project-root/
sudo ./vendor/bin/sake installsake
```
--------------------------------
### Install a Specific Module Version
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Specify a version constraint when installing a module to ensure compatibility. For example, `^2` installs version 2.x.
```bash
composer require silverstripe/blog ^2
```
--------------------------------
### Install Module Using Composer
Source: https://docs.silverstripe.org/en/5/getting_started/composer
After defining the custom repository, install the module using the standard 'composer require' command. Composer will automatically use the specified fork.
```bash
composer require silverstripe/cms
```
--------------------------------
### Install Project Dependencies with Yarn
Source: https://docs.silverstripe.org/en/5/contributing/build_tooling
Installs all JavaScript dependencies for a module using Yarn. Run this command in the module's directory and in the silverstripe/admin directory.
```bash
yarn install
```
--------------------------------
### Install Production Dependencies with Composer
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Use this command on your production server to install only production dependencies and optimize the autoloader. This ensures faster load times and excludes development-specific packages.
```bash
composer install --no-dev -o
```
--------------------------------
### Run Controller via Sake
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Execute any controller using `sake` by passing its relative URL. This example shows running the homepage controller.
```bash
sake /
```
--------------------------------
### Example Commit Message
Source: https://docs.silverstripe.org/en/5/contributing/code
This is an example of a well-formatted commit message that includes a clear prefix, a concise subject line, and detailed explanation of the changes.
```text
FIX Allow multiple iterations of eager-loaded DataLists
Previously, a second iteration would add every relation item to the
relation list a second time - so with each iteration your relation list
count doubled (though it was the same records time and again).
```
--------------------------------
### Install a Silverstripe CMS Module
Source: https://docs.silverstripe.org/en/5/developer_guides/extending/modules
Use this Composer command to install a module from Packagist.org. Ensure you have Silverstripe CMS set up with Composer first.
```bash
composer require silverstripe/blog
```
--------------------------------
### Create New Silverstripe Project
Source: https://docs.silverstripe.org/en/5/contributing/code
Installs a new Silverstripe CMS project from composer, targeting the 5.1.x-dev development branch.
```bash
composer create-project --keep-vcs silverstripe/installer ./your-website-folder 5.1.x-dev
```
--------------------------------
### Protected File Storage Example
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_storage
Shows the directory structure for protected files, which are stored in an 'assets/.protected' folder with hashed subdirectories. This ensures files require permissions to be accessed.
```text
assets/
my-public-folder/
my-public-file.jpg
my-public-file__FitWzYwLDYwXQ.jpg
.protected/
my-protected-folder/
b63923d8d4/
my-protected-file.jpg
my-protected-file__FitWzYwLDYwXQ.jpg
```
--------------------------------
### Create a New Project with Specific Version Constraint
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Install a specific version of Silverstripe CMS, such as the latest patch of the 4.13 release, using version constraints.
```bash
composer create-project silverstripe/installer ./my-project ^4.13
```
--------------------------------
### Install Yarn Package Manager
Source: https://docs.silverstripe.org/en/5/contributing/build_tooling
Installs the Yarn package manager globally using npm. This is a prerequisite for managing JavaScript dependencies.
```bash
npm install -g yarn
```
--------------------------------
### Install Module from Source with Composer
Source: https://docs.silverstripe.org/en/5/developer_guides/extending/modules
Use Composer to require a module directly from its source repository, preserving the local Git repository for development. The version constraint 'dev-main' installs from the main branch.
```bash
composer require my_vendor/module_name:dev-main --prefer-source
```
--------------------------------
### Example Public File Path Structure
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_storage
Illustrates the directory structure for public files within the Silverstripe CMS assets folder. This structure is directly mapped to public URLs.
```text
assets/
my-public-folder/
my-public-file.jpg
```
--------------------------------
### Variant File Storage Example
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_storage
Illustrates how variant files are stored in the same directory as the original file, distinguished by a variant suffix. This is common for resized images or converted formats.
```text
assets/
my-public-folder/
my-public-file.jpg
my-public-file__FitWzYwLDYwXQ.jpg
```
--------------------------------
### Combined [CHILDREN] tag example
Source: https://docs.silverstripe.org/en/5/contributing/documentation
This example demonstrates combining multiple modifiers for the [CHILDREN] tag to list all children as a description list, in reverse order, including folders, and excluding a specific folder.
```Silverstripe Shortcode
[CHILDREN Exclude="How_tos" asList includeFolders reverse]
```
--------------------------------
### Page metadata (frontmatter) example
Source: https://docs.silverstripe.org/en/5/contributing/documentation
This is an example of page metadata used by Gatsby for rendering. It includes title, summary, and icon information, which are used when the page is displayed as a child.
```YAML
---
title: My page title
summary: This summary, the title, and the icon will all be used when rendering this page as a child.
icon: file-alt
---
```
--------------------------------
### Update Project Dependencies
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Run `composer update` to fetch the latest available versions of all installed modules and dependencies according to your `composer.json` constraints. This also updates the `composer.lock` file.
```bash
composer update
```
--------------------------------
### IIS Rewrite Rule for Secure File Serving
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_security
This IIS rewrite rule is designed to serve existing files directly while ensuring non-existent files are processed by the framework. It requires the Rewrite extension to be installed.
```xml
```
--------------------------------
### Create a New Project with Development Files
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Include development fixtures like Behat and PHPUnit configurations by using the --prefer-source option.
```bash
composer create-project silverstripe/installer my-project --prefer-source
```
--------------------------------
### Configure Environment Variables
Source: https://docs.silverstripe.org/en/5/getting_started
Create a .env file in your project root and replace placeholders with your specific database and environment details. This is crucial for connecting to your database and setting the environment type.
```bash
SS_DATABASE_CLASS="MySQLDatabase"
SS_DATABASE_NAME=""
SS_DATABASE_SERVER="localhost"
SS_DATABASE_USERNAME=""
SS_DATABASE_PASSWORD=""
SS_DEFAULT_ADMIN_USERNAME="admin"
SS_DEFAULT_ADMIN_PASSWORD="password"
SS_ENVIRONMENT_TYPE=""
```
--------------------------------
### Build the Database
Source: https://docs.silverstripe.org/en/5/getting_started
Run this command within your project directory to build the database schema after configuring your environment variables. This command is part of the Silverstripe sake tool.
```bash
vendor/bin/sake dev/build
```
--------------------------------
### Run Build Task with Flush via Sake
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Execute the `dev/build` task using `sake` and force a cache flush by passing `flush=1` as an argument.
```bash
sake dev/build "flush=1"
```
--------------------------------
### Loading Local File into DBFile
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_storage
Demonstrates how to load a file from the local filesystem into a DBFile object associated with a DataObject. The file is specified by its temporary path and a target filename.
```php
use App\Model\Banner;
// Image could be assigned in other parts of the code using the below
$banner = Banner::create();
$banner->Image->setFromLocalFile($tempfile['path'], 'my-folder/my-file.jpg');
```
--------------------------------
### Create Silverstripe CMS Project with Composer
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Use this command to create a new Silverstripe CMS project with full git repository information, developer dependencies, and the development version. Replace '5.x-dev' with a specific minor branch like '5.1.x-dev' if targeting a particular release line.
```bash
composer create-project --keep-vcs silverstripe/installer ./my-project 5.x-dev --prefer-source
```
--------------------------------
### Replacing Deprecated Function
Source: https://docs.silverstripe.org/en/5/developer_guides/deprecations
Example of replacing a deprecated function like Director::isDev() with its modern equivalent, SilverStripe\Core\Env::is_dev(), and issuing a deprecation notice.
```php
namespace SilverStripe\Control;
use SilverStripe\Dev\Deprecation;
// ...
class Director
{
// ...
/**
* Returns true if your are in development mode
* @deprecated 4.12.0 Use SilverStripe\Core\Env::is_dev() instead.
*/
public function isDev()
{
Deprecation::notice('4.12.0', 'Use SilverStripe\Core\Env::is_dev() instead');
return Env::is_dev();
}
}
```
--------------------------------
### Schedule BuildTask with Cron
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Configure a cron job to run a Silverstripe CMS `BuildTask` using `sake` every minute. Replace `/your/site/folder/` with the actual path.
```bash
* * * * * /your/site/folder/vendor/bin/sake dev/tasks/MyTask
```
--------------------------------
### List Development Operations via Sake
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Use `sake` to display a list of available development operations by navigating to the `dev/` URL.
```bash
sake dev/
```
--------------------------------
### Create a New Silverstripe Project
Source: https://docs.silverstripe.org/en/5/getting_started/composer
Use Composer to create a new Silverstripe CMS project, downloading the latest stable version by default.
```bash
composer create-project silverstripe/installer my-project
```
--------------------------------
### Add Private VCS Repository to Composer
Source: https://docs.silverstripe.org/en/5/developer_guides/extending/modules
Configure your project's composer.json to include a private Git repository URL. This allows Composer to locate and install modules not published on Packagist.
```json
{
"repositories": [
{
"type": "vcs",
"url": "git@github.com:my_vendor/module_name.git"
}
]
}
```
--------------------------------
### Run Database Rebuild via CLI
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Execute a database rebuild from the command line using the `cli-script.php` entry point. Ensure you are in your project's root directory.
```bash
cd your-project-root/
php vendor/silverstripe/framework/cli-script.php dev/build
```
--------------------------------
### Hint Box Syntax
Source: https://docs.silverstripe.org/en/5/contributing/documentation
Use this syntax to create a hint box for pointing out extra use cases or hints about how to use a feature.
```markdown
> [!TIP]
> ...
```
--------------------------------
### Daemon Process Template
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
A PHP template for creating a daemon process that monitors memory usage and restarts if limits are exceeded. Includes basic loop logic and sleep intervals.
```php
namespace App\CLI;
use SilverStripe\Control\Controller;
class MyProcess extends Controller
{
private static $url_segment = 'my_process';
private static $allowed_actions = [
'index',
];
public function index()
{
// This isn't allowed to be accessed via the browser
if (!Director::is_cli()) {
$this->httpError(401);
}
set_time_limit(0);
while (memory_get_usage() < 32 * 1024 * 1024) {
if ($this->somethingToDo()) {
$this->doSomething();
sleep(1);
} else {
sleep(300);
}
}
}
// ...
}
```
--------------------------------
### Set File Visibility During Creation
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_security
Use the 'visibility' option when setting a file from a local path to specify if it should be protected or public. It's recommended to set visibility early to prevent sensitive content from being exposed.
```php
$object->MyFile->setFromLocalFile($tmpFile['Path'], $filename, null, null, [
'visibility' => AssetStore::VISIBILITY_PROTECTED,
]);
```
--------------------------------
### Reinstall Module from Source
Source: https://docs.silverstripe.org/en/5/contributing/code
Re-installs a specific module from its source using composer, useful when working on a module within an existing project.
```bash
# re-install the module using prefer-source.
# replace / with the module you're making changes to (e.g. silverstripe/framework)
composer reinstall / --prefer-source
```
--------------------------------
### Basic Debugging Utilities
Source: https://docs.silverstripe.org/en/5/developer_guides/debugging
Utilize static methods from the Debug and Backtrace classes for common debugging tasks like displaying variables, logging messages, and inspecting the call stack.
```php
use SilverStripe\Dev\Backtrace;
use SilverStripe\Dev\Debug;
Debug::show($myVariable);
// similar to print_r($myVariable) but shows it in a more useful format.
Debug::message("Wow, that's great");
// prints a short debugging message.
Backtrace::backtrace();
// prints a calls-stack
```
--------------------------------
### Run Production Build
Source: https://docs.silverstripe.org/en/5/contributing/build_tooling
Builds core JavaScript and CSS files in production mode. This command must be run before committing changes to Git.
```bash
yarn build
```
--------------------------------
### Send a Test Email
Source: https://docs.silverstripe.org/en/5/developer_guides/email
Create and send a basic email using the Email class. This snippet can be run as a BuildTask to test email functionality and 'from' address configuration.
```php
use SilverStripe\Control\Email\Email;
$email = Email::create($from, $to, $subject, $body);
$email->send();
```
--------------------------------
### Versioned File Path Structure
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_storage
Illustrates the directory structure for versioned files, where older versions are stored in a .protected folder with content hashes.
```text
assets/
my-file.jpg <- current published file
.protected/
dec83f348d/ <- old content hash of replaced file version
my-file.jpg
b63923d8d4/ <- old content hash of replaced file version
my-file.jpg
```
--------------------------------
### Create Project with Latest Development Version in a Specific Minor Release
Source: https://docs.silverstripe.org/en/5/getting_started/composer
This command creates a new Composer project using the latest development version within a specific minor release of silverstripe/installer, such as 5.0.x-dev. It's ideal for developing bug fixes.
```bash
composer create-project silverstripe/installer ./my-project 5.0.x-dev
```
--------------------------------
### Enable Deprecation Warnings via Configuration
Source: https://docs.silverstripe.org/en/5/developer_guides/deprecations
Use this PHP configuration to enable deprecation warnings programmatically. Ensure the Deprecation class is imported.
```php
// app/_config.php
use SilverStripe\Dev\Deprecation;
Deprecation::enable();
```
--------------------------------
### Pass Arguments to Sake
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Pass parameters to `sake` by appending them as a query string to the controller URL.
```bash
vendor/bin/sake myurl "myparam=1&myotherparam=2"
```
--------------------------------
### Link to Property API Documentation
Source: https://docs.silverstripe.org/en/5/contributing/documentation
This syntax is used to link to the API documentation for a property, including configuration properties, within a class in Silverstripe CMS.
```markdown
[Form->casting](api:SilverStripe\Forms\Form->casting)
```
--------------------------------
### Test Email Sending with BuildTask
Source: https://docs.silverstripe.org/en/5/getting_started/server_requirements
Use this BuildTask to test if emails are being sent correctly from your production environment and to verify the 'from' address setting.
```php
use SilverStripe\Control\Email\Email;
$email = Email::create('no-reply@mydomain.com', 'myuser@gmail.com', 'My test subject', 'My email body text');
$email->send();
```
--------------------------------
### Link to Method API Documentation
Source: https://docs.silverstripe.org/en/5/contributing/documentation
This syntax is used to link to the API documentation for a specific method within a class in Silverstripe CMS.
```markdown
[Form::makeReadonly()](api:SilverStripe\Forms\Form::makeReadonly())
```
--------------------------------
### Set Site URL via Environment Variable
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Specify the site's URL for CLI operations like email sending or static file generation using the `SS_BASE_URL` environment variable.
```bash
SS_BASE_URL="https://www.example.com/base-url"
```
--------------------------------
### Run Development Build
Source: https://docs.silverstripe.org/en/5/contributing/build_tooling
Builds core JavaScript and CSS files in development mode for faster debugging. This command is useful during active development.
```bash
yarn dev
```
--------------------------------
### Enable Deprecation Warnings via .env
Source: https://docs.silverstripe.org/en/5/developer_guides/deprecations
Add this line to your .env file to enable deprecation warnings in development environments.
```dotenv
SS_DEPRECATION_ENABLED=true
```
--------------------------------
### Info Box Syntax
Source: https://docs.silverstripe.org/en/5/contributing/documentation
Use this syntax to create an info box for adding, deepening, or accenting information in the main text. It can be used for background knowledge or to provide links to further information.
```markdown
> [!NOTE]
> ...
```
--------------------------------
### Writing an Asset to a Protected Location
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_security
Use this configuration to write an asset to a protected location within the asset store. Ensure the AssetStore class is singletoned.
```php
$store = singleton(AssetStore::class);
$store->setFromString('My protected content', 'my-folder/my-file.jpg', null, null, [
'visibility' => AssetStore::VISIBILITY_PROTECTED,
]);
```
--------------------------------
### Link to Class API Documentation
Source: https://docs.silverstripe.org/en/5/contributing/documentation
This syntax is used to link to the API documentation for a class, interface, trait, or enum in Silverstripe CMS.
```markdown
[Form](api:SilverStripe\Forms\Form)
```
--------------------------------
### Create New Working Branch
Source: https://docs.silverstripe.org/en/5/contributing/code
Checks out the correct base branch (e.g., 'origin/5.1') and creates a new feature branch for your work.
```bash
# make sure you're starting from the correct branch first
cd vendor//
git checkout --track origin/5.1
# then create your working branch
git checkout -b
```
--------------------------------
### Configure Custom Sendmail Binary
Source: https://docs.silverstripe.org/en/5/developer_guides/email
Specify a custom path and command for the sendmail binary using the MAILER_DSN environment variable. Ensure the path points to a valid sendmail executable.
```bash
# .env
MAILER_DSN="sendmail://default?command=/path/to/mysendmailbinary%20-t"
```
--------------------------------
### Module Composer Configuration
Source: https://docs.silverstripe.org/en/5/developer_guides/extending/modules
Define module metadata and dependencies in composer.json. Ensure 'type' is set to 'silverstripe-vendormodule' for Silverstripe CMS compatibility.
```json
{
"name": "my_vendor/module_name",
"description": "Short module description",
"type": "silverstripe-vendormodule",
"require": {
"silverstripe/framework": "^5.0",
"silverstripe/cms": "^5.0",
"silverstripe/blog": "^5.0"
}
}
```
--------------------------------
### Watch for File Changes and Rebuild
Source: https://docs.silverstripe.org/en/5/contributing/build_tooling
Automatically rebuilds JavaScript and CSS files whenever changes are detected in .js or .scss files. Ideal for rapid iteration.
```bash
yarn watch
```
--------------------------------
### Push Branch to GitHub Fork
Source: https://docs.silverstripe.org/en/5/contributing/code
Push your local branch to your GitHub fork to back up your work and prepare for a pull request.
```bash
cd vendor//
git push pr
```
--------------------------------
### Enable Asset Store Extension via YAML
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_security
Configure Silverstripe CMS to use the custom AssetStore extension. This enables the `updateResponse` method for modifying file HTTP responses.
```yaml
SilverStripe\Assets\Flysystem\FlysystemAssetStore:
extensions:
- App\Extension\AssetStoreExtension
```
--------------------------------
### Configure Protected Assets Path
Source: https://docs.silverstripe.org/en/5/getting_started/server_requirements
Use this environment variable to specify a custom path for protected assets, moving them outside the webroot for enhanced security. This path is relative to the project's public directory.
```bash
# This will be inside your project root, along-side the public/ directory
SS_PROTECTED_ASSETS_PATH="../.protected/"
```
--------------------------------
### Stop Daemon Process via Sake
Source: https://docs.silverstripe.org/en/5/developer_guides/cli
Manage a daemon process using `sake` by stopping it with the `-stop` flag followed by the process name.
```bash
sake -stop my_process
```
--------------------------------
### Publish a Protected File
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_security
Use the AssetStore singleton to publish a file from its protected location to the public assets directory. This involves specifying the filename and its internal hash identifier.
```php
use SilverStripe\Assets\Storage\AssetStore;
$store = singleton(AssetStore::class);
$store->publish('NewCompanyLogo.gif', 'a870de278b475cb75f5d9f451439b2d378e13af1');
```
--------------------------------
### Configure Protected Folder Location
Source: https://docs.silverstripe.org/en/5/developer_guides/files/file_security
Set the path for storing protected assets outside the web root to enhance security. This prevents direct web access to sensitive files.
```bash
SS_PROTECTED_ASSETS_PATH="/sites/myapp/protected"
```