Hi guys,

Can anyone refer me to some very basic and simplified MVC tutorial? Yes, I googled, and read a lot, also did try frameworks, but my goal here is to be able to write and understand the MVC code myself. I'm also new to OOP, so even with tuts that provide the full code I don't get to far.

If MVC philosophy is not too much code when stripped down to the extremely basic example, a bit of code would be appreciated I believe not by me only:)

Any guidance, book recommendation, or even recommendation of different system (PAC or anything else) is welcomed.

Or if no other advice, please do bash my current method of doing websites (I "invented" this myself so I'm always uncertain is it the right way to do it):

index.php is the single entry point that handles everything

1. it checks for get variable, say pageid=contact-us

2. it pulls data for the contact-us page from the database (title, meta tags, page content)

3. it checks is there a file named contact-us.php

4. it renders the html page, with database values embedded using php, like this:

<title><?php echo $title ?></title>
 ..
 ..
 ..
 <div><?php echo $content ?></div>

or if in step 3 it was determined that there is a file of such name, then the file is included instead:

<title><?php echo $title ?></title>
 ..
 ..
 ..
 <div><?php include $pageid.'.php'; ?></div>

This in case that some page needs to have it's own specific php code.

I know it's basic knowledge, which makes it even more important, as I do even some mid-sized websites, and keep doubting my base.

Thanks for reading, thanks for help :)

Member Avatar for diafol

You could use .htaccess to rename (rewrite) the pages so that it at least looks like a normal url. Why do you want to have just the one 'hub' page?

I sometimes produce small sites that use simple templates (non-MVC):

index.php
about.php
something.php
content/index.php
content/about.php
content/something.php
templates/main.php


The template file (main.php) pretty much contains the lot. The top level pages usually just have the one or two lines of php, e.g. meta data and the include('templates/main.php') line. Sometimes additional js is passed via variable.

In the body area of the template I have the

<?php include("content/$thispage");?>

Where $thispage is set in a config file as basename($_SERVER)
Each content file holds the body content for that particular page.
It seems like a lot of phaff, but works really well for small projects.

I used to hold my meta data in a DB, but found that it was really difficult to get hold of the data for editing. Now, as all my sites are bi/multi-lingual, I keep all such data in arrays in language files. This allows very easy editing.

Member Avatar for diafol

You could use .htaccess to rename (rewrite) the pages so that it at least looks like a normal url. Why do you want to have just the one 'hub' page?

I sometimes produce small sites that use simple templates (non-MVC):

index.php
about.php
something.php
content/index.php
content/about.php
content/something.php
templates/main.php


The template file (main.php) pretty much contains the lot. The top level pages usually just have the one or two lines of php, e.g. meta data and the include('templates/main.php') line. Sometimes additional js is passed via variable.

In the body area of the template I have the

<?php include("content/$thispage");?>

Where $thispage is set in a config file as basename($_SERVER)
Each content file holds the body content for that particular page.
It seems like a lot of phaff, but works really well for small projects.

I used to hold my meta data in a DB, but found that it was really difficult to get hold of the data for editing. Now, as all my sites are bi/multi-lingual, I keep all such data in arrays in language files. This allows very easy editing.

For learning OOP with PHP I highly recommend PHP 5 Objects, Patterns, and Practice by Matt Zandstra. I read the first edition of this book and learned a LOT about OOP. The MVC pattern is discussed in the book.

Thank you guys both for help.

Ardav, I do use url rewriting (just didn't show it here to simplify the example)some of the reasons I prefer single entry point:

- being DRY (Don't Repeat Yourself), by typing the code needed on all pages just once (head section, header, footer, menu...)

- easier maintenance and way faster site-wide changes by editing just one file

- easier security management - everyone has to go through the same door

- decreased chances for a bug, typo or any kind of messing up the site

The only downside I see is that if you mess something up in your index file, the mess becomes site wide too:) but then again, you would have just one file to look at and fix, and things are back to normal

As for the meta tags, I'll give your approach a thought, but for me the database approach worked pretty well so far - when I write a CMS, it always includes fields for editing title and meta tags, so looks kinda convenient to me.

madCoder, thanks I'll check it out.

Member Avatar for diafol

Fair one C&A, for a CMS, the DB route may be best.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.