Jump to content

VC?


Andy-H

Recommended Posts

Does a controller necessarily have to invoke a Model or can I just invoke the view straight away or is it bad practice?

 

 


<?php
/**
* Index
*
* Default controller, invokes when not query string is present
*
* @version 1.0
* @package com
*/
namespace com\controllers;
class Index extends Abstract_Controller implements Interface_Controller {
   /**
   * Index::invoke
   * Invoke method required by Interface_Controller, output error message 
   * @access public
   */
   public function invoke(array $params)
   {
      $view = new \com\views\Index;
      $view->output('No method requested in query string');
      return;
   }
}

 

 

Link to comment
Share on other sites

I have changed the output method to a getContents method, which returns rather than echoing so only the controller outputs to the client, still unsure as to whether I need to invoke a model though, thanks for any help.

Link to comment
Share on other sites

Based on just what you have:

 

1. Better to name it class.* instead of *.class.php.

2. A controller shouldn't have a _getDBH() function. Makes more sense to have the model do whatever functionality you need.

3. Views shouldn't return a JSON encoded $data. Just display it on the spot. It can be pretty messy (and annoying) to look at any data that's in string format when you want to debug it later.

Link to comment
Share on other sites

It pretty much depends on what the controller needs to do.  If the controller needs to work with data, that is where models come into play.  All the work you need to do with persisting or serializing data should be in models.  You typically inject the model data into your views, so they can work with that data, but the view simply displays the data, or gathers user input.  The models should always handle the persistinng of data, or querying etc. if that data is in a relational database.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.