CakePHP 4 How To Create Custom Bake Console Command

Reading Time: 5 minutes
3,044 Views

Inside this article we will see the concept of CakePHP 4 How to create custom bake console command. This article contains classified information about creating custom command in cakephp 4.

CakePHP 4 has a rich set of console commands list i.e Bake console commands. In this tutorial we will see how can we develop a user defined console command into bake list.

Learn More –

Let’s get started.

CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.

Custom Commands Steps

To create a custom command we use these following steps. You can follow to create a user defined bake console command in a very easy steps –

  • Use bake command to create custom command
  • Add logics and parameters into Custom command class

To create custom command using bake, this command we can use.

$ bin/cake bake command <CommandClassName>

Let’s see all these things in action.

Create Command (Print Message)

Open project into terminal and run this command into it.

$ bin/cake bake command hello

It will generate a HelloCommand.php file inside /src/Command folder.

Open HelloCommand.php and write this following code into it.

<?php

declare(strict_types=1);

namespace App\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;

/**
 * Hello command.
 */
class HelloCommand extends Command
{
    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
    {
        $parser = parent::buildOptionParser($parser);

        return $parser;
    }

    public function execute(Arguments $args, ConsoleIo $io)
    {
        $io->out("Welcome to Online Web Tutor");
    }
}

Above command will only print a static message to console when we run it.

Back to terminal and type this basic command of bake to list all commands.

$ bin/cake

It will open the list of bake console commands. In this command list, you should see this help command.

Run Command

Type this command to execute it.

$ bin/cake hello

It will display a simple message into console – “Welcome to Online Web Tutor”

Custom Command (With help Arguments)

Here, again we will create the same command but this time we will some help arguments with this. Let’s say we want a name value as an input while executing this command.

Something, Command to use –

$ bin/cake hello sanjay

Output should be like –

Welcome To Online Web Tutor Sanjay

Open HelloCommand.php file from /src/Command folder.

<?php

declare(strict_types=1);

namespace App\Command;

use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;

/**
 * Hello command.
 */
class HelloCommand extends Command
{
    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
    {
        $parser = parent::buildOptionParser($parser);

        $parser->addArgument("name", [
            "help" => "Please give your name"
        ]);

        return $parser;
    }

    public function execute(Arguments $args, ConsoleIo $io)
    {
        $name = $args->getArgument("name");

        $name = ucfirst($name);

        $io->out("Welcome To Online Web Tutor {$name}");
    }
}

Back to terminal and type this command to see help manual of hello command –

We can see from help manual i.e needs to pass a name value with command.

Execute this command to see output –

$ bin/cake hello sanjay

We should see our expected output of the custom command.

We hope this article helped you to CakePHP 4 How To Create Custom Bake Console Command Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.