Home > Backend Development > PHP Tutorial > slim框架接入pysh

slim框架接入pysh

WBOY
Release: 2016-06-23 13:03:40
Original
1126 people have browsed it

由于实际开发中需要查看接口返回值结构和实际的数据样例,每次都写controller再访问url看下是比较麻烦的。

所以如果能像 php -a这样交互的方式调用网站应用中定义的model和function的话,将会极其的方便。

目录结构如下:

app/public/tinker
Copy after login

app为网站具体逻辑实现的部分

public下面有1个index.php为网站的入口

tinker就是我们需要接入pysh,实现REPL的文件了

由于Slim里面需要PATH_INFO的值,但是命令行运行的时候,这个值是不存在的,所以需要在代码中mock一下;代码如下:

#!/usr/bin/env php<?phprequire __DIR__ . '/vendor/autoload.php';$app = require __DIR__ . '/app/bootstrap.php';$app->environment = \Slim\Environment::mock([    'PATH_INFO' => '/playground']);$app->notFound(function () use ($app) {    $path = $app->environment['PATH_INFO'];    echo "Cannot route to $path" . PHP_EOL;    $app->stop();});$app->error(function (\Exception $e) use ($app) {    echo $e . PHP_EOL;    $app->stop();});$app->any('/playground', function () {    $config = new \Psy\Configuration([        'tabCompletion' => true,        'tabCompletionMatchers' => [            new \Psy\TabCompletion\Matcher\ClassNamesMatcher,            new \Psy\TabCompletion\Matcher\ClassMethodsMatcher,            new \Psy\TabCompletion\Matcher\ClassAttributesMatcher,            new \Psy\TabCompletion\Matcher\FunctionsMatcher,        ],    ]);    $shell = new \Psy\Shell();    $shell->run();});$app->run();
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template