Issue
Is there a way to interact with Yii on the command line with a console like rails console
? I'd like to test DB and ActiveRecord calls.
Solution
You can install yii-shell. It's made by the Yii team. It works like rails console
EDIT:
Sorry, I worked from the documentation of yii-shell which is - at the time of this writing - but a promise.
This is how you can get a proper REPL working in Yii 2.
First, we would need to get Psysh. You can install it globally to play with it, but I recommend adding the following line to your composer.json
require-dev: {
// ... some other packages ...
"psy/psysh": "0.7.2"
}
Run composer update
to get this package installed.
Now we need to add this to a controller. The way to call up the break point from a controller is eval(\Psy\sh());
.
Note that this would invoke the console for debugging. So if your app is served with Apache, Nginx, or any other server which is not tied to an interactive console, this is pointless
For this to work, I have served the application using PHP's inbuilt server and Yii's wrapper for it.
In Yii Basic App template...
cd /path/to/application
./yii serve localhost:12345
In any controller, ...say controllers/SiteController.php
public function actionIndex()
{
eval(\Psy\sh()); // <-- debugger point
return $this->render('index');
}
When you access this action via tha URL, it would hang on your browser. If you check back in the console, you would see an interactive shell which should work like rails c
. Checkout the Psysh Documentation for more details. To exit this interactive console, type exit
; this should return control back to PHP's inbuilt server. Do not exit the interactive console with Ctrl-C
as this would close the PHP's inbuilt server also.
In Yii Advanced App template...
Serving the application does not work at the moment since it defaults to serving the contents of console/web
which doesn't exist. I have raised an issue with Yii; you can follow along there if it interests you.
However, if you have console controllers, you can do the same thing we did for the basic app. When you run the console command, you should be presented with the same interactive debug console.
Answered By - iGbanam
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.