Note that there are some explanatory texts on larger screens.

plurals
  1. POPlease critique my first attempt at MVC in PHP
    primarykey
    data
    text
    <p>Well I'm not a huge framework guy but I've been liking what I've been hearing about the whole MVC movement so I thought I'd try to create a simple application in my language of choice (PHP)</p> <p>So I guess the question is: where did I go wrong? I know there is a lot of debate as to how fat the controller/model should be so hopefully we can avoid that, however I am especially curious as to your thoughts on how I fit the datatier in.</p> <p>Also I bought a domain to do some testing so if you want to see it in action you can go to <a href="http://www.omgmvc.com/" rel="noreferrer">www.omgmvc.com</a></p> <p>Firstly, here's my database schema:</p> <pre><code>CREATE TABLE `movies` ( `id` int(11) NOT NULL auto_increment, `movie_name` varchar(255) NOT NULL, `release_date` date NOT NULL, `directors_name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ); INSERT INTO `movies` VALUES (1,'Star Wars', '1977-05-25', 'George Lucas'); INSERT INTO `movies` VALUES (2,'The Godfather', '1972-03-24', 'Francis Ford Coppola'); INSERT INTO `movies` VALUES (3,'The Dark Knight', '2008-07-18', 'Christopher Nolan'); </code></pre> <p>And here's the files:</p> <p><strong>index.php</strong> <em>(controller)</em></p> <pre><code>&lt;?php include('datatier.php'); include('models/m_movie.php'); if (isset($_GET['movie']) &amp;&amp; is_numeric($_GET['movie'])) { $movie = new Movie($_GET['movie']); if ($movie-&gt;id &gt; 0) { include('views/v_movie.php'); } else { echo 'Movie Not Found'; } } else { $movies = Movie::get_all(); include('views/v_list.php'); } ?&gt; </code></pre> <p><strong>datatier.php</strong> <em>(data tier)</em></p> <pre><code>&lt;?php class DataTier { private $database; function __construct() { $this-&gt;connect(); } function __destruct() { $this-&gt;disconnect(); } function connect() { $this-&gt;database = new PDO('mysql:host=localhost;dbname=dbname','username','password'); } function disconnect() { $this-&gt;database = null; } function get_all_from_database($type) { $database = new PDO('mysql:host=localhost;dbname=dbname','username','password'); switch ($type) { case 'movie': $query = 'SELECT id FROM movies'; break; } $movies = array(); foreach ($database-&gt;query($query) as $results) { $movies[sizeof($movies)] = new Movie($results['id']); } $database = null; return $movies; } function get_from_database($type,$id) { switch ($type) { case 'movie': $query = 'SELECT movie_name,release_date,directors_name FROM movies WHERE id=?'; break; } $database_call = $this-&gt;database-&gt;prepare($query); $database_call-&gt;execute(array($id)); if ($database_call-&gt;rowCount() &gt; 0) { return $database_call-&gt;fetch(); } else { return array(); } } } ?&gt; </code></pre> <p><strong>models/m_movie.php</strong> <em>(model)</em></p> <pre><code>&lt;?php class Movie extends DataTier { public $id; public $movie_name; public $release_date; public $directors_name; function __construct($id) { parent::connect(); $results = parent::get_from_database('movie',$id); if ($results == array()) { $this-&gt;id = 0; } else { $this-&gt;id = $id; $this-&gt;movie_name = $results['movie_name']; $this-&gt;release_date = $results['release_date']; $this-&gt;directors_name = $results['directors_name']; } } function __destruct() { parent::disconnect(); } static function get_all() { $results = parent::get_all_from_database('movie'); return $results; } } ?&gt; </code></pre> <p><strong>views/v_list.php</strong> <em>(view)</em></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Movie List&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;table border="1" cellpadding="5" cellspacing="5"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Movie Name&lt;/th&gt; &lt;th&gt;Directors Name&lt;/th&gt; &lt;th&gt;Release Date&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php foreach ($movies as $movie) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;a href="/?movie=&lt;?php echo $movie-&gt;id; ?&gt;"&gt;&lt;?php echo $movie-&gt;movie_name; ?&gt;&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $movie-&gt;directors_name; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $movie-&gt;release_date; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>views/v_movie.php</strong> <em>(view)</em></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;?php echo $movie-&gt;movie_name; ?&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;&lt;?php echo $movie-&gt;movie_name; ?&gt;&lt;/h1&gt; &lt;h2&gt;Directed by &lt;?php echo $movie-&gt;directors_name; ?&gt;&lt;/h2&gt; &lt;h3&gt;Released &lt;?php echo $movie-&gt;release_date; ?&gt;&lt;/h3&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload