October CMS – Execute Console Commands Sequentially

October CMS Logo

In this tutorial, I am going to show you how to execute long running PHP processes sequentially in October CMS from anywhere of your code.

First, you must define a default driver, which is pretty much a handler for managing how to run a queued job, identifying whether the jobs succeeded or failed, and trying the job again. In October CMS, the driver settings are located in the file in ./config/queue.php.

If you are new to this, I recommend to start with the database driver. This driver is good enough to execute your console commands synchronously in the background without slowing down your PHP calls. As a next step, you have to register a custom console command in your Plugin.php file. For example company.pluginname.

For test purposes, I created a console command company.pluginname which will execute the method backupList.

Let’s assume you have a front-end controller http://yourpage.dev/customer/list that calls an API, creates a list and saves it on disk. Executing all these steps can be time-consuming and simply slow down your controller. Instead, you can run such processes in the background with Artisan::queue.

As soon as you hit the controller, artisan will queue a new record in the table jobs and wait until a queue worker executes the job. You just have to make sure that a Queue Worker is running in the background, otherwise jobs won’t get executed.

I recommend to start a queue worker with Supervisor which is well explained on https://medium.com/@rohit_shirke/configuring-supervisor-for-laravel-queues-81e555e550c6

 

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

Laravel – Carbon and Queries

Laravel

Carbon is probably the most effective way to handle DateTime strings flawlessly and implement PHP code that everybody can easily extend or debug.

In the following example, I would like to demonstrate how you can filter a collection in Laravel by using Carbon. As you can see, the below example will simply return all People who have registered in the past two weeks.

The method subWeeks() will do all the work for you and return a valid DateTime string from 14 days ago which can be used for the whereBetween query method. If you prefer using days, you can simply use subDays() instead.

One possible use case could be a front-end controller that handles AJAX requests.