PHP在函数函数内设置变量

时间:2016-02-26 12:29:58

标签: php oop slim slim-3

我需要设置变量的建议。我正在使用Slim Framework。我上课了。

MailboxManager.php

    public function getMailById($id) {
        /* @var $sql PDOStatement */
        $sql = $this->database->prepare("SELECT * FROM appmail_mails WHERE id = :id");
    $sql->bindParam('id', $id);
    $sql->execute();
    $record = $sql->fetch(PDO::FETCH_ASSOC);
    if ($record === false) {
        return null;
    }
    $mail = new Mail($id);
    $mail->setMailContent($record['content']);
    $mail->setMailDate($record['date']);
    $mail->setMailSubject($record['subject']);
    $mail->setMailSize($record['
return $mail;

MailController.php

class MailController
private $id;
public function showMail()
    $mail = ServicesFactory::getMailboxManager()->getMailById($this->id);
    echo "<pre>";
    var_dump($mail);

现在我想用Slim Framework创建路径。在索引php中我放了:

$app->get('/dupa/{id}', function(ServerRequestInterface $request){

    $id = $request->getAttribute('id');
    echo $id;
    $new = new MailController();
    $test = $new->showMail();
    var_dump($test);
});

我应该如何设置id:

$mail = ServicesFactory::getMailboxManager()->getMailById($this->id);

正确得到它? 我每次都试图:

$test = $new->showMail($id)

我得到空。

谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

路由参数作为第三个参数传递给你的闭包

$app->get('/dupa/{id}', function(ServerRequestInterface $request, $response, $args){

    $id = $args['id'];
    echo $id;
    $new = new MailController();
    $test = $new->showMail();
    var_dump($test);
});