Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:56:26+00:00 2026-06-17T07:56:26+00:00

Well, it is technically possible, but would this break the MVC architecture? I’m not

  • 0

Well, it is technically possible, but would this break the MVC architecture?

I’m not sure whether this type of communication is recommended between both controller and model. I will describe it using a simple example and two ways of doing it:

OPTION 1 (model throws exception and controller catches it):

class Controller {
  private $model;

  public function save($data) {
     try {
         $this->model->save($data); 
     } catch (Exception $e) {  
         // handle exception
     }
  }
}

class Model {
  public function save($data) {
     // Call to internal function to save data in BD
     if (! $this->_save($data)) throw new Exception('Error saving data');
  }
}

OPTION 2 (the controller handles the exception completely):

class Controller {
  private $model;

  public function save($data) {
     try {
         if (! $this->model->save($data)) throw new Exception('Error saving data'); 
     } catch (Exception $e) { 
         // handle exception
     }
  }
}

class Model {
  public function save($data) {
     // Call to internal function to save data in BD
     if (! $this->_save($data)) return false;
  }
}

**

EDIT after some responses:

**

These are other ways to solve it based on your suggestions. I hope not to get things too complicated.

OPTION 3 (model handles the exception completely, as Ray said. KingCrunch also suggested to better do it in the model)

class Controller {
  private $model;

  public function save($data) {

     if (! $this->model->save($data)) {
         // possible action: redirect to the form with an error message
     }

  }
}

class Model {
  public function save($data) {
     try {
         if (! $this->_save($data)) throw new Exception('Error saving data'); 
     } catch (Exception $e) { 
         // handle exception
         return false;
     }
     return true; 
  }
}

OPTION 4 (controller gets a custom child exception thrown by the model, as shiplu.mokadd.im said.)

class Controller {
  private $model;

  public function save($data) {
     try {
         $this->model->save($data); 
     } catch (Exception $e) {  
         if ($e instanceof ValidationException) {
            // handle validation error
         }
         elseif ($e instanceof DBStorageException) {
            // handle DB error
         }
     }
  }
}

class Model {
  public function save($data) {
     if (! $this->_validate($data)) {
        throw new ValidationException ('Validation error');
     }
     if (! $this->_save($data)) {
        throw new DBStorageException ('Storage error');
     }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:56:27+00:00Added an answer on June 17, 2026 at 7:56 am

    Model can throw Exception and Controller or View should catch it. Otherwise you never know if everything is working properly down there. So use the first option. But make sure you are throwing properly abstracted Exception that is meaningful to the controller and View.

    To illustrate the above bold line see these two throw statements which are used inside a model.

     throw new Exception('SQL Error: '.$mysqli->error()); // dont use it
     throw new DuplicateFieldException('Duplicate username'); // use this
    

    The second example does not show internal error. Rather it hides it. Controller should never know whats happening inside.

    In your code your tied a single model to a single controller. Controller does not represent a single model. It uses model. And it can use any number of model. So dont tie up a single model with a controller with variable like private $model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not technically a programming question but it does specifically relate to a
Well, technically it is an ASP.net routing question but since I am using MVC
Well, I know technically how to intercept an error, but what should I do
I'm not exactly sure how to ask this, or if it's been asked before...
Note: I think this is technically still code-first but I have an existing database
I'm not really into type-safety as a concept but many consider it important for
Sorry for the non-technical title, but I think it summarizes my question well. If
Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection
well i have a configuration like this in the components part of my config
Well, I have a application which somehow requires some system resources, but how do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.