Question

I'm trying to run an action via CLI. The action is UsersController::test()

So, I run this: php libraries/lithium/console/lithium.php \\app\\controllers\\UsersController test

But I keep running into this error: PHP Fatal error: Call to undefined method lithium\console\Request::get() in /var/www/example.com/libraries/lithium/action/Controller.php on line 154

What am I doing wrong?

Was it helpful?

Solution

See http://li3.me/docs/lithium/console for reference.

As far as I know, it's not possible to call controllers directly from the command line. Although from your error, it seems it's trying to do something.

Instead, create a class that extends from \lithium\console\Command and place that in namespace app\extensions\command. For example, name that class Users. Inside that class, create a method called test() that will be run when you invoke your command via cli.

Now when you run li3 (or lithium.php), it should print all of the available commands and you should see yours in the list. You would call li3 users test and that should run it if you've added lithium/console/li3 to your bin path. Or you could call php -f path/to/lithium.php -- users test which should do the same thing. If you left out test, it will look for a method called run() in your app\extensions\console\Users class and call that.

You could potentially create an instance of your controller and call the test() method, but as you saw, the Request class that is available when running from a web browser is not the same Request class when running from the command-line. It would probably be better to take the business logic in your controller method and move it to another class and then pass in the request parameters from your controller to that method. Then from the console command class, you would similarly call the same centralized method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top