Search code examples
phpjquerycakephpmodal-dialogdirectory-structure

CakePHP - jQuery modals in keeping with Cake's MVC structure


I wish to make use of modals in my app, for which I will use jQuery's UI dialog (unless anyone has better suggestions). I was hoping to keep all modal content strictly within CakePHP framework, if possible, but I'm not sure how to do this.

How would I trigger a modal that uses a controller from /controllers/_modals and a view from /view/_modals, and that still has access to Cake's framework via Ajax calls, including modals?


Solution

  • You shouldn't create separate models or controller just to display content on modal boxes.

    Just think about it, if you want to show your users' shopping cart in a model, the guy who take cares of it is the UsersController. I mean, is the same info but displayed in different ways.

    So, i'd do something like this. I'd keep the controllers the way they are, and, for example, if i want to see what the shopping cart, then you could make the ajax call to that action, and don't display the layout.

    So, your UsersController:

    class UsersController... {
       function shoppingCart(){
       //doing some stuff...
       $this->layout=null;
       //here you could use $this->render(); but it's not necessary
       }
    }
    

    And, in your view, you just make the ajax call (using jquery):

    <?php $shoppingCartUrl = $this->Url(array('controller'=>'users','action'=>'shoppingCart'));
    
    $.ajax({
      url: $shoppingCartUrl,
      context: document.body,
      success: function(data){
        $("#modal").html(data)
      }
    });
    

    Hope it works.