Viewed   99 times

I've been learning Zend and its MVC application structure for my new job, and found that working with it just bothered me for reasons I couldn't quite put my finger on. Then during the course of my studies I came across articles such as MVC: No Silver Bullet and this podcast on the topic of MVC and web applications. The guy in the podcast made a very good case against MVC as a web application architecture and nailed a lot of what was bugging me on the head.

However, the question remains, if MVC isn't really a good fit for web applications, what is?



It all depends on your coding style. Here's the secret: It is impossible to write classical MVC in PHP.

Any framework which claims you can is lying to you. The reality is that frameworks themselves cannot even implement MVC -- your code can. But that's not as good a marketing pitch, I guess.

To implement a classical MVC it would require for you to have persistent Models to begin with. Additionally, Model should inform View about the changes (observer pattern), which too is impossible in your vanilla PHP page (you can do something close to classical MVC, if you use sockets, but that's impractical for real website).

In web development you actually have 4 other MVC-inspired solutions:

  • Model2 MVC: View is requesting data from the Model and then deciding how to render it and which templates to use. Controller is responsible for changing the state of both View and Model.

  • MVVM: Controller is swapped out for a ViewModel, which is responsible for the translation between View's expectations and Models's logic. View requests data from controller, which translates the request so that Model can understand it.

    Most often you would use this when you have no control over either views or the model layer.

  • MVP (what php frameworks call "MVC"): Presenter requests information from Model, collects it, modifies it, and passes it to the passive View.

    To explore this pattern, I would recommend for you begin with this publication. It will explain it in detail.

  • HMVC (or PAC): differs from Model2 with ability of a controller to execute sub-controllers. Each with own triad of M, V and C. You gain modularity and maintainability, but pay with some hit in performance.

Anyway. The bottom line is: you haven't really used MVC.

But if you are sick of all the MVC-like structures, you can look into:

  • event driven architectures
  • n-Tier architecture

And then there is always the DCI paradigm, but it has some issues when applied to PHP (you cannot cast to a class in PHP .. not without ugly hacks).

Saturday, September 24, 2022

If you're validating the data on client side (i.e Javascript validation) which is absolutely not enough and not secure at all, You should implement it in View.

If you're validating data on server side, And your validation does not require application business logic (i.e you're not checking to see if the user has enough credit in his account), You should validate in the controller.

If the validation requires business logic, Implement it inside the model and call it via controller.

Postback validation is not good since it puts lots of pressure and delay, And the only advantage is to the programmer (not to be accounted).

You can use regex for most of validation, Which has the same syntax (almost) on PHP and JS.

Tuesday, December 20, 2022

The controller picks up the view variable in the url and using these determines which view needs to be used. It then sets the view to be used. The view then calls the model to fetch the data it requires and then passes this to the tmpl to be displayed.

Below is a simple setup of how this all works together:


class TestController extends JController

  // default view
  function display() {
    // gets the variable some_var if it was posted or passed view GET.
    $var = JRequest::getVar( 'some_var' );
    // sets the view to someview.html.php
    $view = & $this->getView( 'someview', 'html' );
    // sets the template to someview.php
    $viewLayout  = JRequest::getVar( 'tmpl', 'someviewtmpl' );
    // assigns the right model (someview.php) to the view
    if ($model = & $this->getModel( 'someview' )) $view->setModel( $model, true );
    // tell the view which tmpl to use 
    $view->setLayout( $viewLayout );
    // go off to the view and call the displaySomeView() method, also pass in $var variable
    $view->displaySomeView( $var );



class EatViewSomeView extends JView

  function displaySomeView($var)  {
    // fetch the model assigned to this view by the controller
    $model = $this->getModel();
    // use the model to get the data we want to use on the frontend tmpl
    $data = $model->getSomeInfo($var);
    // assign model results to view tmpl
    $this->assignRef( 'data', $data );
    // call the parent class constructor in order to display the tmpl



class EatModelSomeView extends JModel 

  // fetch the info from the database
  function getSomeInfo($var) {
    // get the database object
    $db = $this->getDBO();
    // run this query
      FROM #__some_table
      WHERE column=$var
    // return the results as an array of objects which represent each row in the results set from mysql select
    return $db->loadObjectList(); 



// loop through the results passed to us in the tmpl
foreach($this->data as $data) {
  // each step here is a row and we can access the data in this row for each column by 
  // using $data->[col_name] where [col_name] is the name of the column you have in your db
  echo $data->column_name;
Tuesday, September 20, 2022

The book Purely Functional Data Structures covers your questions in depth, and includes a great mix of theory and implementations primarily in ML - the appendix also contains Haskell implementations so you should be able to follow along with a bit of extra page turning. It is a pretty good (though difficult in parts) read if you are really interested in a thorough answer to your questions. Having said that I think ephemient gave a superb short answer.

edit: Steven Huwig provided a link to the thesis that the book started as. While I haven't read through it the only big thing missing (judging from the table of contents) are the Haskell implementations.

Friday, September 9, 2022

Work with new OWIN Identity API that wraps everything that you need to work with Application and External sign in cookies like bellow:

public class IdentityAuthenticationManager
    public IdentityAuthenticationManager();
    public IdentityAuthenticationManager(IdentityStoreManager storeManager);

    public string ClaimsIssuer { get; set; }
    public string RoleClaimType { get; set; }
    public IdentityStoreManager StoreManager { get; set; }
    public string UserIdClaimType { get; set; }
    public string UserNameClaimType { get; set; }

    public virtual void Challenge(HttpContextBase context, string authenticationType, string redirectUrl);
    public virtual Task<bool> CheckPasswordAndSignIn(HttpContextBase context, string userName, string password, bool isPersistent);
    public virtual Task<bool> CreateAndSignInExternalUser(HttpContextBase context, string loginProvider, IUser user);
    public virtual IEnumerable<Microsoft.Owin.Security.AuthenticationDescription> GetExternalAuthenticationTypes(HttpContextBase context);
    public virtual Task<ClaimsIdentity> GetExternalIdentity(HttpContextBase context);
    public virtual Task<IList<Claim>> GetUserIdentityClaims(string userId, IEnumerable<Claim> claims);
    public virtual Task<bool> LinkExternalIdentity(ClaimsIdentity id, string userId, string loginProvider);
    public virtual Task SignIn(HttpContextBase context, string userId, bool isPersistent);
    public virtual Task SignIn(HttpContextBase context, string userId, IEnumerable<Claim> claims, bool isPersistent);
    public virtual Task<bool> SignInExternalIdentity(HttpContextBase context, ClaimsIdentity id, string loginProvider);
    public virtual void SignOut(HttpContextBase context);
    public virtual bool VerifyExternalIdentity(ClaimsIdentity id, string loginProvider);

And The following shows the login code for the ASP.NET MVC template:

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    if (ModelState.IsValid)
        // Validate the user password
        if (await AuthenticationManager.CheckPasswordAndSignIn(HttpContext, model.UserName, model.Password, model.RememberMe))
            return RedirectToLocal(returnUrl);

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);

For more information visit this.

Friday, September 9, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :