A question about MVC

So I’ve recently read up on MVC and how it benefits you. None of the articles actually explain how one would use it in a simple application. So I want to ask a question about MVC.

So I have a file called “config.php” and it’s in one of my folders named includes. I have the config.php included in all of my works and in the “config.php” file, it has all of my constants.

Is this like an MVC? Or is it different? I know that MVC needs Model, View, and Controller, but in my application; I have all the data defined in the config file so all of my database connection and all of the files that I need to include are all in the config file.

So let’s say I wanted to get the full path to my root folder, well in my config file I have a constants called FILE_PATH which can be called in PHP like this

<?php
include(FILE_PATH . "index.php");
echo FILE_PATH . "index.php";
?>

Then when I echo out FILE_PATH . “index.php”, it becomes the full path such as /home/yourwebsitename/public_html/index.php

Would this be considered MVC? Or is it just a way to make my life easier by including my functions in one file and just pulling what I really need into a file?

No, this is not MVC at all. This is just db connection stuff in a config file. The actual pulling things out of the DB would be in the model, which would reference the config file for db information.

This is an amazing video to get a good understanding of MVC:

It’s an 11 part series, but I think the first 5 or so should set you on your way.

@mawburn
Thanks, I’ll watch all 11 parts and try to understand it more.

Np. Good luck! I went through the first 7 or so, they are 20-45 minutes and I was coding along with him which made it about an hour or so each. I had used MVC frameworks before this and had a pretty good understanding, but there is nothing like actually creating one from scratch to fully understand it.

@mawburn
Yeah I know what you mean. I’ve used cake like once and I didn’t really understand it because it was already an application. I thought it would be an application that teaches you about MVC.

I’ve heard about Laravel too. Same thing, not much explaining. So that’s why I tried to research it myself so I can understand it more, but the articles I was reading only tells you what MVC is and not how it looks like in code view.

So thanks for sharing a video with me. I’ll look into it and see where I’ll end up. I really want to switch to MVC since my application really needs it because I looked at my application and I saw that I might need it because I’m probably going to have a big database which will break my application if I don’t make my application more efficient.

I’d also suggest reading From Flat PHP to Symfony2. I really like this article for learning MVC because it starts with plain old spaghetti PHP and it takes you step-by-step through refactoring it into MVC.

I also like it because it boils MVC down to its very simplest. It’s doesn’t add any extraneous requirements. For example, some frameworks require that the first URL segment always correspond to a controller class name and the second URL segment to a method name. But that behavior is unecessary and not mandated by MVC.

(Halfway into the article, it starts introducing Symfony components, and you can stop reading at that point if you wish.)

@therockers,

I think Cake PHP is not a good Php Framework to start because it is too sophisticated and complex for a beginner to MVC principals.

There are numerous other frameworks available that I would suggest investigating to see which has the best forum for newbies.

Once you have selected a framework and got the hello world running, try adding a menu to other pages that link back to your main page. Afterwards try adding your database and extract content to display on your pages.

Installing other frameworks is usually quite easy and you could try others until you find one that is suitable.

Have fun:)

@John_Betong @Jeff_Mott
Thanks I’ll try my best to understand it.

I have a question though while I’m at it. So I have an old script I wanted to test on the first link that @mawburn gave me. I went through the walkthrough and I had it all good and working until I used my old code. My old codes uses $this-> as well and when I use my full codes, it gives me this error.

Note: I have blanked out the function name.

Fatal error:  Call to a member function ______ on a non-object

When I use

 function __construct() {
 }

And I wrap it around my function, it doesn’t give me the error, but it stops my original script from working. Why doesn’t it allow me to use 2 different $this->?

Hi therockers,

When you get the “Call to a member function xxx on a non-object” error it’s because there’s no object assigned to the variable that you’re trying to call the method on, i.e:

$foo = "test";
$foo->bar();
// Will throw a 'Call to a member function bar() on a non-object' error

I’m not sure what you mean by this - can you post some more code to show what you’re trying to do?

It sounds like you’re trying to convert procedural code to OO code, but without seeing more of your code it’s difficult to see what might be giving you problems.

@fretburner
I don’t know. When ever I get that error, it’s because it doesn’t match the data that’s being pulled out or that there were some variables missing. But that isn’t the case for this so I don’t know what’s wrong.

But here’s a small portion of my code. I mean it works in its own environment without the MVC right now, but with MVC it gives me the member function error.

<?php
class Database {

    /* Database connection here */

    function foo() {
         echo "Is foo";
    }

}

$db = new Database;

$db->foo();
?>

I didn’t use the full tutorial @mawburn gave me, but I watched the whole 11 part video. I only used up to part 2 on this code because he was starting to make his own login system and my application already has it’s own login system so I didn’t want to use his.

Part 2 of the video doesn’t have $db-> yet, but later on he does add it in which I stopped using the tutorial for.

And what I meant by $this-> was just $db-> because $db-> was used multiple times. The portion of the code is not what matters, but when I use 2 different source codes, the implementation doesn’t seem to work out great. That’s why I’m seeing the member function error because without MVC framework, the application works great by itself and I have no member function error, but when I start to shift to MVC framework and use the application codes, it starts to give me that error. When I comment out the lines that it’s giving errors to. The member function error is gone, but now I can’t login to my original application.

It sounds like what might be happening is that you’re instantiating the Database class in one part of your app, and then trying to access it somewhere else where the variable is out of scope, such as within a function or class method:

$db = new Database;
$db->foo(); // works

class MyController {

    public function bar()
    {
        $db->foo(); // throws error, as $db is out of scope
    }

}

@fretburner
How could I be so stupid. I forgot that my application is in the root folder and the MVC framework is in a folder called “framework” so when I type in http://localhost/framework/ it didn’t work because I didn’t include my database in the index of framework. That’s why I kept seeing the no member error.

Thanks a lot. I knew it wasn’t because my old application was interfering with MVC.

I was trying to test it out so when I actually did decide to use MVC, I would just simply replace my application in the root folder with the framework folder. All I just need to do if I decide to switch is just take off …/includes/config.php and have includes/config.php.

If I happen to fall into more errors, I can come back and ask again.

I’ve started to work my way through the tutorial videos (post #2), it has made me realise that how I had what I thought was MVC was not proper MVC (it was probably closer to normal N-tier

1 Like

Learning is sometimes a painful process. :-\

As you watch the videos, be careful not to feel locked into some of the implementation choices. There’s actually a lot of room for improvisation when fleshing out the MVC pattern into a full-fledged framework.

For the bare-bones description of (Web) MVC, for example, Martin Fowler describes it like this:

The basic responsibilities of a Page Controller are:

  • Decode the URL and extract any form data to figure out all the data for the action.
  • Create and invoke any model objects to process the data. All relevant data from the HTML request should be passed to the model so that the model objects don’t need any connection to the HTML request.
  • Determine which view should display the result page and forward the model information to it.

And Paul Jones describes it like this:

  • User Agent sends a Request
  • Router/Dispatcher invokes Controller
  • Controller manages flow, invokes Model(s) that encapsulate domain
  • Controller assigns values to View template
  • Controller invokes View and sets into Response (with headers)

I’m going for sessions being stored in the database anyway. For user groups, I’m planning on storing them in a table using nested sets as there’ll be different tiers of users, as you go up ther tiers users will have greater powers.

Once I’ve worked my way through the tutorials i’ll look through the code and decide on where i’m going to do things differently. I might well use the idea from the tutorial about custom javascript loading for CSS as my site will have some CSS that will be common to absolutely every page and some that will only be used in certain parts, to save having a massive CSS file.

I plan on learning javascript properly (not any library like for example jquery as with any given library chances are that i won’t actually use half of it and it’s pointless loading however many k in size jquery is when I might only need 1/4 of it in the end