我在使用官方创建所有内容之后编写了这个路由器:
$ php composer.phar create-project slim/slim-skeleton [my-app-name]
这是我的routes.php
文件
<?php
require_once(__DIR__."/../bootstrap.php");
// Routes
class OwnsPost
{
/**
* Example middleware invokable class
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke($request, $response, $next)
{
if($request->getQueryParams() && $request->getQueryParams()['pid']){
$pid = intval($request->getQueryParams()['pid']);
if($pid == 0){
$this->logger->info("illegal pid call");
return false;
}
$cpost = get_post($pid);
if($cpost->post_author != get_current_user()){
$this->logger->info("wrong current user, tried accessing postid " . $cpost->ID . " with user ". get_current_user());
return false;
}
}else{
$this->logger->info("illegal pid call");
return false;
}
// $response->getBody()->write('BEFORE');
$response = $next($request, $response);
// $response->getBody()->write('AFTER');
return $response;
}
}
$app->get('/campaignedit/setcharitable/{id}', function ($request, $response, $id) {
// Sample log message
$this->logger->info("setcharitable '/' route " . $id);
// Render index view
return $this->renderer->render($response, 'index2.php', $id);
})->add( new OwnsPost() );
$this->logger
适用于路由部分,但不适用于中间件部分。
我得到了
致命错误:在非对象中调用成员函数info() 第33行的/var/www/html/japi/src/routes.php
答案 0 :(得分:1)
中间件类中没有成员变量$logger
。所以,先添加一个。
protected $logger;
接下来,添加一个接受$logger
作为参数的构造函数。
public function __construct($logger)
{
$this->logger = $logger;
}
最后,当您初始化中间件时,请传递Logger实例。
$ownsPost = new OwnsPost($this->logger);
答案 1 :(得分:-2)
我显然需要打电话给
create: function(req, res) {
var user_id = req.token.sid;
req.allParams().user = user_id;
Message.create(req.allParams(), function(err, message) {
if (err) return res.json(err.status, {err: err});
});
});