October CMS – How To Use Models In Custom Components

October CMS Logo

Components in October CMS can be used for custom output, such as JavaScript snippets, dynamic content from a web service or models. Components can also handle AJAX requests or other incoming data. In this tutorial, I will simply show you how to register a new component and output a list with records from the users table.

First of all, you have to register a new component in your Plugin.php file. For example, users.

Create the following structure within the components folder which must contain the file Users.php and users/default.htm.

Below you will find the content for the default.htm file which will basically output a list of all users.

The Users.php file only requires a method getPeople and a reference to the new model, called UsersModel.

In order to access a model, you must create one within the models folder.

For test purposes, I simply return all records from the users table.

Now you can use the new component anywhere in your template files. I recommend to use [users] in your parent template file, because it will allow you to use the component in your child templates.

After a page reload, should you immediately see the list.

October Cms Custom Component

For more information, check out https://octobercms.com/docs/cms/components. The source code of this tutorial can be found on https://github.com/tobias-forkel/october-cms-playground