For Developers: Running OpenCart In The Command Line (Updated)

[Updated on: March 28th, 2017]

As the title already suggests, this post is aimed mainly at OpenCart developers.

This post will be a short tutorial that will teach you how to build a custom controller for your OpenCart store and run it in the Linux command line with the help of oc_cli - an OpenCart extension developed by iSenseLabs.

 

What is oc_cli?

In short, oc_cli is an OpenCart extension which allows you to run OpenCart controllers directly from the Linux command line. It is especially useful for admin panel controllers, because it bypasses the standard OpenCart admin panel login/permission checks.

oc_cli works only for OpenCart 2.2.0.0 - 2.3.0.2. For installation instructions and how to use it, please visit the extension repo in GitHub: https://github.com/iSenseLabs/oc_cli

Install oc_cli in your OpenCart store and come back to this tutorial. No original files will be overwritten.

Disclaimer: oc_cli is a free extension and you agree to use it at your own risk. We do not take responsibility for any unforeseen misbehaviors of your system. Let's move on…

 

Creating the controller

We will build a custom OpenCart controller which we'll run from the command line. Using your favorite text editor, create the following file in your OpenCart installation:

admin/controller/common/tutorial.php

As the contents of the file insert the following:

<?php
class ControllerCommonTutorial extends Controller {
    public function test() {
		if (!defined("OPENCART_CLI_MODE") || OPENCART_CLI_MODE === FALSE) {
			echo "Intruder alert."; exit;
        }

        oc_cli_output("Hello, world!");
    }
}
?>

First, notice the piece of code saying if (!defined("OPENCART_CLI_MODE") || OPENCART_CLI_MODE === FALSE)

This is used to check if the controller is run from oc_cli. It will exit all requests, which were not made with oc_cli.

The rest of the controller is any kind of code you like. In our case, we use the function "oc_cli_output" which is defined in oc_cli to output a simple message to the console.

 

Time for testing

Now save the file and let’s see what happens if we first access it from a browser. Go to:

http://<your-web-server>/admin/index.php?route=common/tutorial/test

If you are not already logged in, you should see the following:

 

Since the controller is placed in the admin folder, we are immediately shown the login page, which is great. We want to use our controller only in the command line.

Now let's run it in the Linux command line and see the result:

$ php ./oc_cli.php admin common/tutorial/test
$ Hello, world!

 

Final notes

That's all there is to it - simple and straightforward. As you can see, oc_cli can help you develop command-line modules for OpenCart. This can be a useful feature if, for example, you wish to easily develop a CRON job which uses the OpenCart MVC architecture.

Let us know in the comments if you like it. We are open to your constructive suggestions and questions.

Join 11,000+ subscribers receiving actionable E-commerce advice

* Unsubscribe any time

Trending blogs

comments powered by Disqus