1. Go to this page and download the library: Download codechap/yii3-claude-code library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codechap / yii3-claude-code example snippets
use Codechap\Yii3ClaudeCode\ClaudeCodeInterface;
final class MyController
{
public function __construct(
private readonly ClaudeCodeInterface $claude,
) {}
public function actionAsk(): string
{
$response = $this->claude->query('What is the capital of France?');
return $response->getResult();
}
}
return [
'codechap/yii3-claude-code' => [
// Path to the claude binary. Empty string = auto-detect via PATH.
'binaryPath' => '',
// Default model: 'sonnet', 'opus', or 'haiku'.
'model' => 'sonnet',
// Default system prompt sent with every query.
'systemPrompt' => '',
// Maximum number of agentic turns (null = unlimited).
'maxTurns' => null,
// Tools the CLI is allowed to use.
'allowedTools' => [],
// Process timeout in seconds.
'timeout' => 300,
// Environment variables to unset in the subprocess (recursion prevention).
'envUnset' => ['CLAUDECODE', 'ANTHROPIC_API_KEY'],
// Anthropic API key (null = use subscription auth).
'apiKey' => null,
// Custom environment variables passed to the subprocess.
'envSet' => [],
],
];
use Codechap\Yii3ClaudeCode\Model;
$response = $claude
->withModel(Model::Opus)
->query('Explain quantum computing');
$response = $claude
->withJson()
->query('List 3 colors as a JSON array');
$array = $response->toArray(); // Decoded JSON
// Start a conversation
$r1 = $claude
->withJson()
->query('What is the capital of France?');
// Continue with session ID
$r2 = $claude
->withJson()
->withSessionId($r1->getSessionId())
->query('And Germany?');
// Or continue the most recent conversation
$r3 = $claude
->withContinue()
->query('What about Italy?');
$response = $claude
->withSystemPrompt('Reply in haiku form only.')
->query('Describe the ocean');
$response = $claude
->withAllowedTools(['Read', 'Grep', 'Glob'])
->query('Find all TODO comments in this project');
$response = $claude
->withWorkingDirectory('/path/to/project')
->query('Describe this codebase');