Cakephp 3.3 |How to execute script using Console

Author - Parminder Kaur

In Cakephp, console applications are useful in running background processes and can be run without access to web browser. The CakePHP console provides a framework for creating shell scripts.To run the cakephp functions, first of all create a shell to use in console.

For example, create a TestShell in the application’s src/Shell directory and create file TestShell.php and add below code to it:

namespace App\Shell;
use App\Controller\UsersController;
use Cake\Console\Shell;
class TestShell extends Shell
{
public function main(){
$Users = new UsersController();
Users->hello();
}
}

Suppose I have to call a function written inside any controller then call that function in the main() function inside Shell class (In above example hello() function of usersController called inside main() function of TestShell Class).

To run the script using a console, From your application directory path, run:

In Terminal run the command:

bin/cake Test

In windows command line run the following command:

C:\xampp\php\php.exe "bin\cake.php" Test

So we pass Shell Class Name (here Test) as a parameter to cake.php under bin folder in your application which will run the functionality.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *