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 8657833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:37:09+00:00 2026-06-12T15:37:09+00:00

So while tinkering around with my own home built MVC framework I noticed something

  • 0

So while tinkering around with my own home built MVC framework I noticed something that looked in need of some refactoring.

If I have a function call in the view that calls a function residing in the model and that function takes session/post/get vars for parameters. Is it best to pass those vars as arguments in the function call or just access them in the function that’s in the model?

Code in the view:

$model = $this->model; // Shortcut to the model
$vaildator = $this->model->validator; // Shortcut to the Validator object in the model
$btnPressed = $this->model->btnPressed; // Shortcut to the btnPressed flag var in the model

<label for="firstName">First Name: <span class="<?php echo $model->setReq($_POST['firstName'], 'First name'); // Set the class of the span tag the holds the 'required' text ?>">(required)</span></label>
<input type="text" name="firstName" id="firstName" title="First name" value="<?php echo htmlspecialchars($btnPressed ? $_POST['firstName'] : 'First name'); // Echo the user's entry or the default text ?>" maxlength="50" />
<?php if($btnPressed) $vaildator->valName($_POST['firstName'], true, true, 'First name'); // Check for errors and display msg if error is found ?>

Code in the model:

// If the field is empty or still contains the default text, highlight the 'required' error msg on the input field by changing the msg's class
// Note: used only for input fields that are required
public function setReq($postVar, $defaultText)
{
    $className = 'required';

    if($this->btnPressed)
    {
        $className = $postVar == '' || $postVar == $defaultText ? 'reqError' : 'required';
        return htmlspecialchars($className);
    }
    else
    {
        return htmlspecialchars($className); 
    }
}

The reason I ask is because putting the arguments in the function calls in the view makes the view seem logic heavy but doing it the other way, accessing session/get/post vars in the model, seems a bit hacky and would make the code not very reusable. Thanks for any advice!

  • 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-12T15:37:10+00:00Added an answer on June 12, 2026 at 3:37 pm

    The solution is: decoupling.

    Basically, pass the POST variables in the function call.

    Why? Because the logic will stay separated (the controller will use whatever part of request it needs and then will call model), plus it will be a lot easier to test (you will be able to just pass fake arguments instead of faking $_POST and $_GET variables or doing some other fancy things). It will also be easier to debug.

    In other words, decoupling will ease your work.

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

Sidebar

Related Questions

While reading this class BitmapFactory I noticed that almost all methods inside are static.
I was trying to optimize a program I'm tinkering with, when I noticed that
I'm tinkering with some code and I realized something I never knew. A normal
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
While refactoring my code base I found a piece of code which I'd like
I'm tinkering with an iPad app that (like many iPad apps) doesn't use the
I've been tinkering with the html5 canvas element for a while now. It is
I have a private project I've been tinkering with for a while. I'm thinking
I have been tinkering on NodeJS for a while now. I still don't seem
I'm trying to pick a web framework that is Java based to start a

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.