Implementing Slugs for SEO-Friendly URLs
This section covers adding a slug field to a model and using it in page URLs for better SEO and user experience.
1. Database and Model Setup
Add a
slugcolumn to your database table using the Builder plugin.- Type:
String - Nullable:
true.
Important
The column must be nullable initially to avoid SQL errors when saving the schema, as existing records won't have a value for this new column.
- Type:
Add a
slugfield to your model's backend form (fields.yaml).- Navigate to your model, then to the "Fields" tab.
- Add a Text field with the field name
slug.
2. Auto-Generating Slugs
- In the
slugfield's Advanced tab, configure the Preset option.- Source Field: The field the slug should be based on (e.g.,
nameortitle). - Preset Type:
slug.
- Source Field: The field the slug should be based on (e.g.,
- This will automatically generate a URL-friendly slug whenever the source field is typed into or updated.
Note
For existing records, you will need to manually open each one and re-save it (or just clear and re-type the title) to trigger the slug generation.
3. Updating URLs and Links
Change the Details Page URL:
- Open the page that displays a single record (e.g.,
movie-single). - Update its URL from
/movies/:idto/movies/:slug.
- Open the page that displays a single record (e.g.,
Configure the
recordDetailsComponent:- Identifier Value: Set to
{{ :slug }}to fetch the record using the URL's slug parameter. - Key Column: Set to
slugto tell the component which database column to match against.
- Identifier Value: Set to
Update Links in the List View:
- In the component that lists the records (e.g.,
recordList), ensure the link to the details page is generated using theslugattribute, not theid.
- In the component that lists the records (e.g.,
Adding Pagination to a Record List
This section details how to enable and correctly configure pagination for a list of records.
1. Enabling Pagination
- In the CMS, open the page containing your
recordListcomponent. - In the component's settings, find the Pagination section.
- Records per page: Set the number of items you want to show on each page (e.g.,
1). - Page number: Define the URL parameter for the page number (e.g.,
{{ :page }}).
2. Configuring an Optional URL Parameter
- After enabling pagination, update your list page's URL. For example, change it from
/moviesto/movies/:page?.
Tip
The question mark (
?) at the end of the URL parameter (:page?) makes it optional. This is crucial for preventing a "Page not found" error when a user visits the base URL (/movies) without a page number.
- With an optional parameter, the following URLs will work correctly:
/movies(defaults to the first page)/movies/2/movies/3
3. Styling Pagination Links
- The
recordListcomponent automatically injects the necessary HTML for pagination links (<ul>,<li>,<a>tags). - You can apply custom CSS to this HTML to style the pagination controls to match your website's design.
