我可以 "return" 来自 class 的两个对象用于我的路线发送到我的树枝模板吗?
Am I able to "return" two objects from a class for my route to send to my twig template?
我是 slim PHP 微框架和 twig 模板系统的新手。
我想知道有没有什么方法可以从 PHP 函数中 "return" 两个对象,这样我的文章就不需要两个函数(例如 getArticle、getComments)?
我知道有一种方法可以用数组来做,但是有像我想做的那样用对象吗?见下文。
当前 class:
<?php
public function getArticle($id){
$stmt = $this->app->engine->rows('SELECT null FROM articles WHERE id = :id', [':id' => $id]);
if($stmt > 0){
return $this->app->engine->testing('SELECT * FROM articles WHERE id = :id', [':id' => $id]);
}else{
return false;
}
}
?>
我想要达到的目标:
<?php
public function getArticle($id){
$stmt = $this->app->engine->rows('SELECT null FROM articles WHERE id = :id', [':id' => $id]);
if($stmt > 0){
$article = new stdClass();
$article->main = $this->app->engine->testing('SELECT * FROM articles WHERE id = :id', [':id' => $id]);
$article->comments = $this->app->engine->testing('SELECT * FROM articles_comments WHERE article_id = :id', [':id' => $id]);
return 'both of the above to $article of route';
}else{
return false;
}
}
?>
路线:
<?php
$app->get('/articles/:id', function ($id) use ($app) {
$article = new stdClass();
$article = $app->user->getArticle($id);
$article->comments = $app->user->getComments($id);
$app->render('article.html', ['article' => $article]);
});
?>
我想要达到的目标:
<?php
$app->get('/articles/:id', function ($id) use ($app) {
$article = $app->user->getArticle($id);
$article->main = 'use the returned $article->main';
$article->comments = 'use the returned $article->comments';
$app->render('article.html', ['article' => $article]);
});
?>
Twig 模板:
{{ article.main.title }}
{{ article.comments.message }}
您没有 2 个对象您有 1 个对象 $article
具有 2 个属性。只需 return $article;
,它将具有 $article->main
和 $article->comments
属性:
$article = new stdClass();
$article->main = $this->app->engine->testing('SELECT * FROM articles WHERE id = :id', [':id' => $id]);
$article->comments = $this->app->engine->testing('SELECT * FROM articles_comments WHERE article_id = :id', [':id' => $id]);
return $article;
然后:
$article = $app->user->getArticle($id);
// print_r($article);
// $article->main and $article->comments are already part of $article
$app->render('article.html', ['article' => $article]);
我是 slim PHP 微框架和 twig 模板系统的新手。
我想知道有没有什么方法可以从 PHP 函数中 "return" 两个对象,这样我的文章就不需要两个函数(例如 getArticle、getComments)?
我知道有一种方法可以用数组来做,但是有像我想做的那样用对象吗?见下文。
当前 class:
<?php
public function getArticle($id){
$stmt = $this->app->engine->rows('SELECT null FROM articles WHERE id = :id', [':id' => $id]);
if($stmt > 0){
return $this->app->engine->testing('SELECT * FROM articles WHERE id = :id', [':id' => $id]);
}else{
return false;
}
}
?>
我想要达到的目标:
<?php
public function getArticle($id){
$stmt = $this->app->engine->rows('SELECT null FROM articles WHERE id = :id', [':id' => $id]);
if($stmt > 0){
$article = new stdClass();
$article->main = $this->app->engine->testing('SELECT * FROM articles WHERE id = :id', [':id' => $id]);
$article->comments = $this->app->engine->testing('SELECT * FROM articles_comments WHERE article_id = :id', [':id' => $id]);
return 'both of the above to $article of route';
}else{
return false;
}
}
?>
路线:
<?php
$app->get('/articles/:id', function ($id) use ($app) {
$article = new stdClass();
$article = $app->user->getArticle($id);
$article->comments = $app->user->getComments($id);
$app->render('article.html', ['article' => $article]);
});
?>
我想要达到的目标:
<?php
$app->get('/articles/:id', function ($id) use ($app) {
$article = $app->user->getArticle($id);
$article->main = 'use the returned $article->main';
$article->comments = 'use the returned $article->comments';
$app->render('article.html', ['article' => $article]);
});
?>
Twig 模板:
{{ article.main.title }}
{{ article.comments.message }}
您没有 2 个对象您有 1 个对象 $article
具有 2 个属性。只需 return $article;
,它将具有 $article->main
和 $article->comments
属性:
$article = new stdClass();
$article->main = $this->app->engine->testing('SELECT * FROM articles WHERE id = :id', [':id' => $id]);
$article->comments = $this->app->engine->testing('SELECT * FROM articles_comments WHERE article_id = :id', [':id' => $id]);
return $article;
然后:
$article = $app->user->getArticle($id);
// print_r($article);
// $article->main and $article->comments are already part of $article
$app->render('article.html', ['article' => $article]);