0

With cakephp i'm making a simple slideshow.

In my PagesController i write the home action as follows:

public function home(){
    // slider
    $this->loadModel('CmsStoreSlider');
    $cmsStoreSlider = $this->CmsStoreSlider->find('all')->toArray();
    $this->set(compact('cmsStoreSlider'));
    $this->set('_serialize', ['cmsStoreSlider']);
}

... and in my Pages/home.ctp i try to call $cmsStoreSlider, but without luck.

<?php foreach ($cmsStoreSlider as $cmsStoreSlider): ?>
           <img src="<?= $cmsStoreSlider->image; ?>" alt="Imagem do slider">
<?php endforeach; ?>

The images are not displaying because cakephp says it invalid argument supllied for foreach. I made a var_dump and says $cmsStoreSlider is an undefined variable.

Any Solutions. i'm new to cake and maybe something is missing in my logic.

Ps. The Entity and table model are created.

André Castro
  • 1,527
  • 6
  • 33
  • 60

1 Answers1

0

Cakephp by default takes Pages controller's display actions as a home page. display function manages pages and subpages itself that is why you are getting error. Either you can change your default home page in your /config/routes.php

 $routes->connect('/', ['controller' => 'Pages', 'action' => 'home']);

OR

Remove code from display action

class PagesController {
    function display()
    {
        // default controller code here
        // your custom code here
    }
}
Sehdev
  • 5,486
  • 3
  • 11
  • 34
  • No. That is not the problem. If i delete the foreach and just make a simple var_dump($cmsStoreSlider) it says undefined variable. The problem is home function inside PagesController.php not passing $cmsStoreSlider to home.ctp. – André Castro Aug 28 '18 at 11:48
  • Are you using display function of PagesController for your website home page? – Sehdev Aug 28 '18 at 12:00
  • Yes. I am using display function and below that one home() function. – André Castro Aug 28 '18 at 12:06
  • 1
    You have to overwrite display function to pass the variable. Please following any one solution from this link https://stackoverflow.com/questions/51838952/how-to-use-this-autorender-false-in-cakephp-3-6-10/51840289#51840289 – Sehdev Aug 28 '18 at 12:09
  • Yes. That's the answer. I overwrite display function and now it works. Image is displaying fine. Thank you @Sehdev. – André Castro Aug 28 '18 at 12:13
  • @AndréCastro Welcome and don't forget to accept the answer. It will help the others. :) – Sehdev Aug 28 '18 at 12:15