wheels generate snippets
Generate code snippets and boilerplate code for common Wheels patterns.
Synopsis
wheels generate snippets [pattern] [options]
wheels g snippets [pattern] [options]Description
The wheels generate snippets command creates code snippets for common Wheels patterns and best practices. It provides ready-to-use code blocks that can be customized for your specific needs, helping you implement standard patterns quickly and consistently.
Arguments
pattern
Snippet pattern to generate
Shows available patterns
Options
--list
List all available snippets
true/false
false
--category
Filter by category
authentication, model, controller, view, database
All categories
--output
Output format
console, file
console
--path
Output file path (required when output=file)
Any valid file path
--customize
Show customization options
true/false
false
--create
Create custom snippet template
true/false
false
--force
Overwrite existing files
true/false
false
Available Snippets
List All Snippets
wheels generate snippets --listOutput:
Available Snippets
Authentication:
- login-form - Login form with remember me
- auth-filter - Authentication filter
- password-reset - Password reset flow
- user-registration - User registration with validation
Model:
- soft-delete - Soft delete implementation
- audit-trail - Audit trail with timestamps
- sluggable - URL-friendly slugs
- versionable - Version tracking
- searchable - Full-text search
Controller:
- crud-actions - Complete CRUD actions
- api-controller - JSON API controller
- nested-resource - Nested resource controller
- admin-controller - Admin area controller
View:
- form-with-errors - Form with error handling
- pagination-links - Pagination navigation
- search-form - Search form with filters
- ajax-form - AJAX form submission
Database:
- migration-indexes - Common index patterns
- seed-data - Database seeding
- constraints - Foreign key constraints
Next steps:
1. Generate a snippet: wheels g snippets <pattern-name>Authentication Snippets
Login Form
wheels generate snippets login-formGenerates:
#startFormTag(action="create")#
#textField(objectName="user", property="email", label="Email")#
#passwordField(objectName="user", property="password", label="Password")#
#checkBox(objectName="user", property="rememberMe", label="Remember me")#
#submitTag(value="Login")#
#endFormTag()#Note: This is a basic snippet. You can customize it by saving to a file and editing:
wheels generate snippets login-form --output=file --path=app/views/sessions/new.cfmAuthentication Filter
wheels generate snippets auth-filterGenerates:
function init() {
filters(through="authenticate", except="new,create");
}
private function authenticate() {
if (!StructKeyExists(session, "userId")) {
redirectTo(route="login");
}
}Password Reset
wheels generate snippets password-resetGenerates:
function requestReset() {
user = model("User").findOne(where="email='#params.email#'");
if (IsObject(user)) {
token = Hash(CreateUUID());
user.update(resetToken=token, resetExpiresAt=DateAdd("h", 1, Now()));
// Send email with token
}
}User Registration
wheels generate snippets user-registrationGenerates:
#startFormTag(action="create")#
#textField(objectName="user", property="firstName", label="First Name")#
#textField(objectName="user", property="email", label="Email")#
#passwordField(objectName="user", property="password", label="Password")#
#submitTag(value="Register")#
#endFormTag()#Model Patterns
Soft Delete
wheels generate snippets soft-deleteGenerates:
function init() {
property(name="deletedAt", sql="deleted_at");
beforeDelete("softDelete");
}
private function softDelete() {
this.deletedAt = Now();
this.save(validate=false, callbacks=false);
return false;
}Audit Trail
wheels generate snippets audit-trailGenerates:
function init() {
property(name="createdBy", sql="created_by");
property(name="updatedBy", sql="updated_by");
beforeSave("setAuditFields");
}
private function setAuditFields() {
if (StructKeyExists(session, "userId")) {
if (this.isNew()) this.createdBy = session.userId;
this.updatedBy = session.userId;
}
}Sluggable
wheels generate snippets sluggableGenerates:
function init() {
property(name="slug");
beforeSave("generateSlug");
}
private function generateSlug() {
if (!len(this.slug) && len(this.title)) {
this.slug = lCase(reReplace(this.title, "[^a-zA-Z0-9]", "-", "all"));
}
}Versionable
wheels generate snippets versionableGenerates:
function init() {
property(name="version", default=1);
beforeUpdate("incrementVersion");
}
private function incrementVersion() {
this.version = this.version + 1;
}Searchable
wheels generate snippets searchableGenerates:
function search(required string query) {
return findAll(where="title LIKE '%#arguments.query#%' OR content LIKE '%#arguments.query#%'");
}Controller Patterns
CRUD Actions
wheels generate snippets crud-actionsGenerates:
function index() {
users = model("User").findAll();
}
function show() {
user = model("User").findByKey(params.key);
}
function create() {
user = model("User").create(params.user);
if (user.valid()) {
redirectTo(route="user", key=user.id);
} else {
renderView(action="new");
}
}API Controller
wheels generate snippets api-controllerGenerates:
function init() {
provides("json");
}
function index() {
users = model("User").findAll();
renderWith(data={users=users});
}Nested Resource
wheels generate snippets nested-resourceGenerates:
function init() {
filters(through="findParent");
}
private function findParent() {
user = model("User").findByKey(params.userId);
}Admin Controller
wheels generate snippets admin-controllerGenerates:
function init() {
filters(through="requireAdmin");
}
private function requireAdmin() {
if (!currentUser().isAdmin()) {
redirectTo(route="home");
}
}View Patterns
Form with Errors
wheels generate snippets form-with-errorsGenerates:
#errorMessagesFor("user")#
#startFormTag(action="create")#
#textField(objectName="user", property="firstName", label="First Name")#
<cfif user.errors("firstName").len()>
<span class="error">#user.errors("firstName").get()#</span>
</cfif>
#submitTag(value="Submit")#
#endFormTag()#Pagination Links
wheels generate snippets pagination-linksGenerates:
<cfif users.totalPages gt 1>
<nav>
<cfif users.currentPage gt 1>
#linkTo(text="Previous", params={page: users.currentPage - 1})#
</cfif>
<cfloop from="1" to="#users.totalPages#" index="pageNum">
#linkTo(text=pageNum, params={page: pageNum})#
</cfloop>
<cfif users.currentPage lt users.totalPages>
#linkTo(text="Next", params={page: users.currentPage + 1})#
</cfif>
</nav>
</cfif>Search Form
wheels generate snippets search-formGenerates:
#startFormTag(method="get")#
#textField(name="q", value=params.q, placeholder="Search...")#
#submitTag(value="Search")#
#endFormTag()#AJAX Form
wheels generate snippets ajax-formGenerates:
#startFormTag(action="create", id="userForm")#
#textField(objectName="user", property="name")#
#submitTag(value="Submit")#
#endFormTag()#
<script>
$("#userForm").submit(function(e) {
e.preventDefault();
$.post($(this).attr("action"), $(this).serialize());
});
</script>Database Snippets
Migration Indexes
wheels generate snippets migration-indexesGenerates:
t.index("email");
t.index(["last_name", "first_name"]);
t.index("email", unique=true);
t.index("user_id");Seed Data
wheels generate snippets seed-dataGenerates:
execute("INSERT INTO users (name, email) VALUES ('Admin', '[email protected]')");Constraints
wheels generate snippets constraintsGenerates:
t.references("user_id", "users");
t.references("category_id", "categories");Custom Snippets
Create Custom Snippet
wheels generate snippets --create my-custom-snippetThis creates a directory structure in app/snippets/my-custom-snippet/:
my-custom-snippet/
└── template.txtYou can then edit the template file and use your custom snippet:
wheels generate snippets my-custom-snippetOutput Options
Output to Console (Default)
wheels generate snippets login-form
# or explicitly:
wheels generate snippets login-form --output=consoleSave to File
wheels generate snippets api-controller --output=file --path=app/controllers/Api.cfcUse --force to overwrite existing files:
wheels generate snippets api-controller --output=file --path=app/controllers/Api.cfc --forceCustomization Options
wheels generate snippets [pattern] --customizeShows available customization options for snippets.
Filter by Category
List snippets from a specific category:
wheels generate snippets --list --category=model
wheels generate snippets --list --category=authentication
wheels generate snippets --list --category=controller
wheels generate snippets --list --category=view
wheels generate snippets --list --category=databaseBest Practices
Review generated code: Customize for your needs
Understand the patterns: Don't blindly copy
Keep snippets updated: Maintain with framework updates
Share useful patterns: Contribute back to community
Document customizations: Note changes made
Test generated code: Ensure it works in your context
Use consistent patterns: Across your application
See Also
wheels generate controller - Generate controllers
wheels generate model - Generate models
wheels scaffold - Generate complete resources
Last updated
Was this helpful?

