Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - Just Layout?

I want to set $this->layout to json in the controller action.

In the json layout, there will be a line saying $this->Javascript>object(); which will parse through the data given to it by the controller, and output the jSON.

However, creating a new view file for each jSON request, eg. recipe_view, ingredient_view isn't necessary, I just need a layout.

Is there a way to bypass the view file altogether and have just the layout, without the notorious Missing View! error?

like image 243
Kieran Avatar asked Jan 18 '23 20:01

Kieran


2 Answers

@pleasedontbelong's solution works. You can also create a layout and view for ajax.

Your layout can be something like this:

<?php echo $content_for_layout;?>

And then you can create an ajax view like this:

<?php echo $this->Js->object($result);?>

And then from your controller...

public function savecontent(){
    $this->autoRender = false;
    $this->set('result', false);

    if(!empty($this->data)){
        $data = $this->data;

        //Do something with your data

        //send results to view
        $this->set('result', $myNewData);
    }

    $this->render(null, 'ajax','/ajax/ajax');
}
like image 192
Scott Harwell Avatar answered Jan 28 '23 17:01

Scott Harwell


hmmm it should be something like this: (not tested)

function action(){
    $this->autoLayout = $this->autoRender = false;

     // your code

    $this->render('/layouts/json');
}

Hope this helps

like image 24
pleasedontbelong Avatar answered Jan 28 '23 15:01

pleasedontbelong