PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] Is there a way to bring up a Yii console like 'rails console'?

 January 09, 2022     yii, yii2     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing