0

I am a CakePHP beginner and I think I must be missing some essential basic knowledge. What I'm trying to do is to have a link in my View file that, when clicked, causes execution of a Model method. I'm trying to use the CakePHP Blog Tutorial as a starting point.

My view file (Sites/view.ctp) includes the following links:

<tr><td class="label">Actions</td><td><?php echo $this->Form->postLink('Sync Pages', array('action' => 'sync', $site['Site']['id']), array('confirm' => 'Are you sure you want to try that?')); ?> | <?php echo $this->Html->link('Edit', array('action' => 'edit', $site['Site']['id'])); ?></td></tr>

My controller file (SitesController.php) includes the following functions:

// Delete function copied from the Cake tutorial    
public function delete($id) {
    if ($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }

    if ($this->Site->delete($id)) {
        $this->Session->setFlash(__('The site with id: %s has been deleted.', h($id)));
        return $this->redirect(array('action' => 'index'));
    }
}

// My failing function (results in "Missing View" error)
public function sync($id) {     
    if ($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }

    if ($this->Site->syncPages($id)) {
        $this->Session->setFlash(__('The site with id: %s has been synced.', h($id)));
        return $this->redirect(array('action' => 'view', $id));
    }
}

And my model file (Site.php) includes the following draft of a function:

public function syncPages() {

    $cms = $this->cms;

    if ($this->cms == 'Wordpress')
    {
        // ... do stuff to retrieve data from a remote Wordpress database and save it to the local CakePHP app database
    }
}

But, as noted above, I can't get that syncPages() function to execute at all. When I click on the Sync link, I get the "Missing View" error. Where am I going wrong? Thanks in advance for your help!

1

1 Answer 1

4

Add $this->layout = $this->autoRender = false to your sync function in the controller.

2
  • This is correct. I'd assume you (alison) need to know why it is correct though. CakePHP is a dumbed down framework that takes most of the work away from you. It expects that if you are working on a function named 'test' that a view named 'test.ctp' also exists. It doesn't care that you are redirecting - it still expects this file exist. Kai's code above tells the framework that you aren't going to be loading a layout and to not autoLoad the view like it does by default.
    – skrilled
    Nov 15, 2013 at 18:14
  • Thanks so much, Kai and skrilled! It seems that $this->autoRender = false alone does the trick to disable both layout and view (now I need to go read about what the difference is between the two... ;-) I guess it must be the case that the autoRendering is turned off by default for the built-in "delete" functionality, right?
    – codebird
    Nov 15, 2013 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.