Skip to content

Instantly share code, notes, and snippets.

@mrdziuban
Last active August 21, 2019 10:20
Show Gist options
  • Save mrdziuban/7c3cffc52c41f0bb7577 to your computer and use it in GitHub Desktop.
Save mrdziuban/7c3cffc52c41f0bb7577 to your computer and use it in GitHub Desktop.
How to use PsySH with a Yii application
<?php
return array(
'eraseDuplicates' => true,
'historySize' => 100,
);
<?php
ini_set('memory_limit', -1);
$yiidir = dirname(__FILE__).'/../../../yii/framework/';
$appdir = dirname(__FILE__).'/../../';
require_once($yiidir.'yii.php');
error_reporting(E_ALL ^ (E_STRICT|E_NOTICE));
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
$_SERVER['SCRIPT_FILENAME'] = $yiidir.'index.php';
$_SERVER['SERVER_PORT'] = 80;
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['SERVER_NAME'] = 'localhost';
Yii::createWebApplication($appdir.'config/main.php');
$version = Yii::getVersion();
echo <<<EOD
Yii Interactive Tool v1.1 (based on Yii v{$version})
Please type 'help' for help. Type 'exit' to quit.
EOD;

Using PsySH (GitHub) as the Yii shell

  • Issue the following commands from the command line in your Yii application directory:
    • chmod +x shell
    • mkdir extensions/yiishell
    • wget -O extensions/yiishell/psysh psysh.org/psysh
    • chmod +x extensions/yiishell/psysh
  • Copy the file init.php below to extensions/yiishell/init.php and update any paths to work with your application configuration
  • Copy the file config.php below to extensions/yiishell/config.php. Change or add to the config array with the options you'd like to use (available options)
  • Now you can start PsySH with the command extensions/yiishell/psysh extensions/yiishell/init.php --config extensions/yiishell/config.php
    • Alternatively, for an easy way to start the shell, copy the shell script below to your application directory, then run ./shell
#!/bin/sh -x
./extensions/yiishell/psysh ./extensions/yiishell/init.php --config ./extensions/yiishell/config.php
@cornernote
Copy link

Thanks for this guide! I wrote a version for Yii2 here:
https://gist.github.com/cornernote/ccf7e62d965497369185

Almost exactly the same, just copied the code from yii cli script into init.php and removed $application->run().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment