我如何能够获得Slim 3中Slim 3中的所有 get/ put/ post变量?
苗条2,
$allGetVars = $app->request->get();
$allPutVars = $app->request->put();
$allPostVars = $app->request->post();
Run Code Online (Sandbox Code Playgroud)
我怎么能在Slim 3中做到这一点?
例如, http://example.com/books/1?title=hello&content=world
我怎样才能在PARAMS title和content小井3呢?
苗条2,
$title = $app->request->get('title');
$content = $app->request->get('content');
Run Code Online (Sandbox Code Playgroud)
我怎么能在Slim 3中做到这一点?
我在我的虚拟盒子上创建了linux(Centos).当我ssh到它,我尝试创建符号链接(在我的共享文件夹上具有完全访问权限):
ln -s path/folder/example myFolder
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ln: creating symbolic link `myFolder': Protocol error
Run Code Online (Sandbox Code Playgroud)
这有什么理由或解决方案吗?
如果我有多对多的关系,那么用它的sync方法更新关系是非常容易的.
但是我会用什么来同步一对多的关系呢?
posts:id, namelinks:id, name, post_id在这里,每个Post可以有多个Links.
我想将与数据库中特定帖子相关联的链接与输入的链接集合(例如,从我可以添加,删除和修改链接的CRUD表单)同步.
应删除数据库中不存在于我的输入集合中的链接.应更新数据库和输入中存在的链接以反映输入,并且仅在我的输入中存在的链接应作为新记录添加到数据库中.
总结所需的行为:
我们已经知道如何notFoundHandler在Slim 3中添加自定义404 :
$container['notFoundHandler'] = function ($c) {
return function ($request, $response) use ($c) {
return $c->view->render($response, 'pages/404.html.twig')
->withStatus(404)
->withHeader('Content-Type', 'text/html');
};
};
Run Code Online (Sandbox Code Playgroud)
我想在我的一条路线中手动触发.
在Slim 2中,我们能够做类似的事情$app->notFound().Slim 3中的等价物是什么?
我想从1d numpy数组(或向量)中选择多个非相邻范围.
假设:
>>> idx = np.random.randint(100, size=10)
array([82, 9, 11, 94, 31, 87, 43, 77, 49, 50])
Run Code Online (Sandbox Code Playgroud)
这当然有效:
>>> idx[0:3]
array([82, 9, 11])
Run Code Online (Sandbox Code Playgroud)
这可以通过单个索引获取:
>>> idx[[0,3,4]]
array([82, 94, 31])
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要的选择范围0:3,以及7:?
我试过了:
>>> idx[[0:3,7:]]
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以做到这一点,还是我需要单独生成它们并连接?
我有一个应用程序,它包含一个用PHP编写的服务器端REST API,以及一些客户端Javascript,它使用这个API并使用它生成的JSON来呈现页面.所以,一个非常典型的设置.
REST API提供的数据是"不可信的",因为它从数据库中获取用户提供的内容.因此,例如,它可能会获取如下内容:
{
"message": "<script>alert("Gotcha!")</script>"
}
Run Code Online (Sandbox Code Playgroud)
显然,如果我的客户端代码直接将其呈现到页面的DOM中,我就创建了一个XSS漏洞.因此,此内容需要首先进行HTML转义.
问题是,在输出不受信任的内容时,我应该逃避服务器端或客户端的内容吗?即,我的API应该返回原始内容,然后使客户端Javascript代码有责任转义特殊字符,或者我的API应该返回"安全"内容:
{
"message": "<script>alert('Gotcha!');<\/script>"
}
Run Code Online (Sandbox Code Playgroud)
那已经逃脱了?
一方面,似乎客户端不应该担心来自我的服务器的不安全数据.另一方面,有人可能会争辩说,当我们确切地知道如何使用数据时,应该在最后一刻尽可能地转义输出.
哪种方法是正确的?
注意:有很多关于处理输入的问题,是的,我知道客户端代码总是可以被操作.这个问题是关于输出数据从我的服务器,可能不值得信赖.
更新:我调查了其他人在做什么,似乎有些REST API倾向于发送"不安全"的JSON.Gitter的API实际上发送了两个,这是一个有趣的想法:
[
{
"id":"560ab5d0081f3a9c044d709e",
"text":"testing the API: <script>alert('hey')</script>",
"html":"testing the API: <script>alert('hey')</script>",
"sent":"2015-09-29T16:01:19.999Z",
"fromUser":{
...
},"unread":false,
"readBy":0,
"urls":[],
"mentions":[],
"issues":[],
"meta":[],
"v":1
}
]
Run Code Online (Sandbox Code Playgroud)
请注意,他们在text密钥中发送原始内容,然后在密钥中发送HTML转义版本html.IMO,这不是一个坏主意.
我已经接受了答案,但我不相信这是一个干枯的问题.我想鼓励进一步讨论这个主题.
我在我的网站上有一个表格,如下所示:
<form id="myform" action="" method="">
<input type="text" name="name[1][first]">
<input type="text" name="name[2][first]">
<input type="text" name="name[3][first]">
</form>
Run Code Online (Sandbox Code Playgroud)
我想简单地获取所有数据并将其发送到Web服务,所以有这个js:
$fields = $('#myform').serializeArray();
Run Code Online (Sandbox Code Playgroud)
问题是,它创建了json,输入名称中显示了所有括号,因此我得到一个解析错误.
我如何使用serializeArray并获得正确的json?
我希望看到的结果格式是这样的:
{
"name": {
"1": {
"first": "val1"
},
"2": {
"first": "val2"
},
"3": {
"first": "val3"
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我想在中间件类中获取当前I路由的名称.以前(在Slim 2.*中)你可以像这样获取当前路线:
$route = $this->app->router->getCurrentRoute();
但是这个功能已经在3.0版的Slim中被删除了.我在以下__invoke方法中找到了以下代码Slim\App:
// Get the route info
$routeInfo = $request->getAttribute('routeInfo');
/** @var \Slim\Interfaces\RouterInterface $router */
$router = $this->container->get('router');
// If router hasn't been dispatched or the URI changed then dispatch
if (null === $routeInfo || ($routeInfo['request'] !== [$request->getMethod(), (string) $request->getUri()])) {
$request = $this->dispatchRouterAndPrepareRoute($request, $router);
$routeInfo = $request->getAttribute('routeInfo');
}
Run Code Online (Sandbox Code Playgroud)
这表明,当前的路线被存储为属性routeInfo的Request.但似乎我的自定义中间件类在设置属性之前被调用(通过该$this->dispatchRouterAndPrepareRoute($request, $router);方法).因为呼叫$request->getAttribute('routeInfo')解决了NULL.
所以我的问题是; 如何从中间件函数/类中获取当前路由(或路由名称)?
或者我应该只复制上面的代码Slim\App?
我正在尝试使用Slim 3 PHP Framework下载文件.Slim 2相当直接,因为我确信Slim 3也是如此,但我只是不明白.
任何帮助,将不胜感激.根据这里的文档:http://www.slimframework.com/docs/objects/response.html 我从这里添加了包:https: //github.com/guzzle/psr7
所以我的代码在这一点看起来像:
$app->get('/worksheet/download/{filename}', function ($request, $response, $args) {
error_log("__________________________");
$fileName = $args['filename'];
error_log($fileName);
$newStream = new \GuzzleHttp\Psr7\LazyOpenStream($fileName, 'r');
$newResponse = $response->withHeader('Content-type', 'application/octet-stream')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Disposition', 'attachment; filename=' . basename($fileName))
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', filesize($fileName))
->withBody($newStream);
return($newResponse);
});
Run Code Online (Sandbox Code Playgroud) 我正在使用SLIM FrameworkLaravel Eloquent ORM用于REST API.最近我遇到了一个问题too many connections.
在一个请求URI期间,我需要进行多次调用Get并Set调用mySql DB.这将打开我所做的每个数据库事务的连接.我想避免这种情况.现在,mysql连接池有200个线程.
我的API预计将有超过1000个并发调用,并且在当前环境中,40%的调用将失败(使用jMeter测试).
我的想法是,对于一个API调用,我的应用程序应该只使用一个连接线程,并将MySql连接池增加到大约1000到1500之间.这是一个糟糕的方法吗?
使用Eloquent ORM,我的数据库连接由Capsule管理.
我应该使用Singleton方法和API请求中的任何后续调用进行第一次连接,应该使用相同的线程吗?
这是我的数据库连接管理器:
use Illuminate\Database\Capsule\Manager as Capsule;
/**
* Configure the database and boot Eloquent
*/
$capsule = new Capsule;
$capsule->addConnection($databaseConfig['mysql']);
// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$dispatcher = new Dispatcher(new Container);
$capsule->setEventDispatcher($dispatcher);
$capsule->setAsGlobal();
$capsule->bootEloquent();
Run Code Online (Sandbox Code Playgroud)
解决这个问题的最佳方法是什么?
UPDATE
我正在尝试另一种方法来建立持久连接.但是,在完成对作业的调用之后,持久连接仍未关闭.即使打电话DB::Disconnect也无济于事.
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use …Run Code Online (Sandbox Code Playgroud)