I want to connect articles to pages but i get error last screen.
Asked
Active
Viewed 295 times
-2
-
1https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question – mybrave Apr 07 '20 at 16:25
-
1Your template file is named `home`, but the controller action is called `homepage`? – Greg Schmidt Apr 07 '20 at 17:35
-
1Don't post code as screenshouts – Salines Apr 08 '20 at 09:56
2 Answers
0
In your home page action
public function homepage() {
$this->loadModel('Articles');
$lastArticles = $this->Articles->find('all', ['limit' => 3, 'order' => 'Articles.created DESC'])->toArray();
$this->set('lastArticles', $lastArticles);
}
View::set
expects 2 parameters, one being the name of the variable in the view and the 2nd being the values of that variable for the view.
0
You are in the PageController.php, I never used that controller for something else than the display() method. I don't know if what you're doing is the intended use.
If you want to list your articles I'd suggest you set that up in the ArticlesController.php.
//in src/Controller/ArticlesController.php
public function home() {
$lastArticles = $this->Articles->find('all', ['limit' => 3, 'order' => 'Articles.created DESC']);
$this->set('lastArticles', $lastArticles);
}
Then you need to create a view template named corresponding to the method name in the controller (see Naming Conventions) - for the method I posted it's "home.php".
//in templates/Articles/home.php
<?php foreach ($lastArticles as $lastArticle): ?>
//.. your fields ..
<?php endforeach; ?>

ndru
- 83
- 1
- 10