Running commands attempts to load shell instead

Hi,

I’m attempting to create a simple Command using an example from the CakePHP documentation found here.

I bake a command using bin/cake bake command Hello, thus creating a file called HelloCommand.php inside src/Command/.

The file contains the exact code the above link contains. However on running the command from console with the following command:

bin/cake hello

I get the following error:

Exception: Shell class for "Hello" could not be found. in [X:\xampp\htdocs\tools\vendor\cakephp\cakephp\src\Console\ShellDispatcher.php, line 327]

Why is it attempting to open a shell instead of the command. Am I missing something? If so I find it pretty unclear in the documentation.

Using CakePHP Version 3.6.4

Did you add changes in PATH ?
https://book.cakephp.org/2.0/en/console-and-shells.html#the-cakephp-console
“Adding cake to your system path”

You linked the 2.X documentation, but I’m on 3.X. On 3.X, I don’t believe you need to add the executable path to the PATH system variable. Instead you cd into your app folder, and run bin/cake.

Nevertheless, I added X:\xampp\htdocs\tools\bin to my System Variable called PATH, to no avail. Still the same error.

I can run shells fine without issues. I made a shell called ToolsShell. When I run bin/cake tools everything works fine. It’s only when I try to run a command, I have issues.

Please, clarify :
You have some thing like :

class ToolsShell extends AppShell {
function hello(){}
}

And
bin/cake tools hello - Doesn’t works ?

Hi, thanks.

Sorry for being unclear.

I have a file called HelloCommand.php inside src\Command folder.

Inside that file is the following code: https://gist.github.com/mentisy/79097cc825d6f4411263c07799b70755

File structure: http://tinypic.com/r/307xd2c/9

On the terminal, I’ve traversed using cd into my project folder. I run the command bin/cake Hello.

According to the documentation that should output Hello World. But I get the error shown in the top post.

Edit:

The reason I brought up the ToolsShell example, was to show that running Shells work perfectly according to documentation, but I understand Shell are deprecated.

I have this file inside src\Shell: https://gist.github.com/mentisy/6df9031b3027eb81f3c5dd2c83445a1e

When I run bin/cake Tools prolongedCheckouts, I get printed out all the rows I get using $this->Model->find().

So in essence: Shells work, Commands do not.

Mentis,

Did you ever find an answer on this? I have exactly the same problem.

I’m not sure I ever found out what the issue was, but I’m having no trouble creating commands as of now. I’ve kept upgrading CakePHP as new versions are released, so that might have solved the issue for me.

I’m currently using 3.8.0 on one app, and 3.7.2 on another, where adding commands works like a charm. Maybe this was fixed in the 3.7 release. What version are you using?

3.8

Guess I need to double check my setup.

3.8.4 specifically. Now I’ve baked a completely new project and tested that one at 3.8.4. Commands work fine.

So, I have something that persisted from my old code (moved forward from 3.4) that is causing the problem.

Entering grim-sleuth-mode.

at some point some code changed in bin dir which needs to be updated manually while it might or might not be it, it can be some clue

1 Like

Thanks Graziel. That was indeed a problem.

Additionally, config/requirements.php and src/Application.php were added and I needed to manually add those files.

Commands are working properly now.

there is also webroot/index.php file that will need update if you didnt have Application.php and note that in requirements.php there is note
You can empty out this file, if you are certain that you match all requirements.

I have been having the same problem trying to follow the documentation in the cookbook - I was seeing the text of the php file being echoed to the console, then the ‘command not found’ error.
It turns out I was missing a “<?php” at the top of the file…

I thought I’d write this down in case anyone else hits the same problem.