Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

MVC


Milad Abolhassani
M
V
C
Introduction
MVC is the idea that you have three different pieces that work
in unison to form a complex application.
What is MVC?
MVC on the Web
The idea is that you have a  controller  that controls the launch
of applications within the framework based on arguments in
the request.
History
1970s
Trygve Reenskaug
xerox parc


Smalltak-80
MVA        Model–view–adapter
HMVC    Hierarchical model–view–controller
MVP        Model–view–presenter
MVVM   Model-View-ViewModel
Alternatives
Usage
    Backend
        Frameworks
            CodeIgniter
            Zend
            CakePHP
            Laravel
            JSF
            Django

   
Client side
           AngularJS
           Ember.js
           Backbone
<html> <!-- Required for all HTML pages -->
        <head>
                <title>The Top Bar Title</title>
        </head>
        <body>
                Some information
        <br> <!-- A line break -->
                <!-- A Comment Tag -->
        <?  // Denotes the start of PHP processing
                echo "The Date and Time is: ";  // print a string, end with a semicolon
                $mydata = "July 29, 2004"; // Assign a variable
                echo $mydata; // print the string variable
        ?>

        </body>
</html> <!-- Closing tag, required -->
Dirty Code!

Example: small shop
  • Database
  • UI
  • Backend
Dirty Code
DRY
Reduce code complexity
Code Reuse
Increased Flexibility
Several developer
More structural
Suitable for future implementations
Rapid application development
Why MVC
Learning process
Not sutiable for extra-small applications
Disadvantages of MVC
Model
Lowest level of the application
Responsible for maintaining and
manipulation data.
View
The View provides different ways to present the
data received from the model. They may be templates
where that data is filled. There may be several different
views and the controller has to decide which one to use
Controller
Usually the controller will call the appropriate model
for the task and then selects the proper view.
it is the link between the view and model
responible for receiving and responding to events
part of the application that handles user interaction

Workflow
  1. user interacts with the user interface
  2. a controller handles the input events from the user interface
  3. the controller access the model
  4. a view uses the model to generate an user interface
  5. view waits for further user interactions

<?php
class Blog extends CI_Controller {

    public function index()
    {
        echo 'Hello World!';
    }
}
?>
application/controllers/
blog.php
example.com/index.php/blog/
Hello World!

example.com/index.php/blog/index/
Hello World!

<?php
class Blog extends CI_Controller {

    public function index()
    {
        echo 'Hello World!';
    }

    public function comments()
    {
        echo 'Look at this!';
    }
}
?>
example.com/index.php/blog/comments/

 <?php
class Products extends CI_Controller {

    public function shoes($sandals, $id)
    {
        echo $sandals;
        echo $id;
    }
}
?>
example.com/index.php/products/shoes/sandals/123
sandals123
application/views/
<html>
<head>
<title>My Blog</title>
</head>
<body>
    <h1>Welcome to my Blog!</h1>
</body>
</html>
<?php
class Blog extends CI_Controller {

    function index()
    {
        $this->load->view('blogview');
    }
}
?>
<?php

class Page extends CI_Controller {

   function index()
   {
      $data['page_title'] = 'Your title';
      $this->load->view('header');
      $this->load->view('menu');
      $this->load->view('content', $data);
      $this->load->view('footer');
   }

}
?>
Multiple view
<?php
class Blog extends CI_Controller {

    function index()
    {
        $data['title'] = "My Real Title";
        $data['heading'] = "My Real Heading";

        $this->load->view('blogview', $data);
    }
}
?>
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
    <h1><?php echo $heading;?></h1>
</body>
</html>
Send data to view
class Blogmodel extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }
}

SELECT column_name,column_name FROM table_name where ...;
class Blog_controller extends CI_Controller {

    function blog()
    {
        $this->load->model('Blog');

        $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}
Controller
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbcollat'] = "utf8_general_ci";
Database configs
RewriteEngine On
RewriteRule ^tutorials/([^/\.]+)/?$ tutorials.php?id=$1 [L]

index.php?c=products&m=view&id=345
Htaccess!
http://code.tutsplus.com/tutorials/mvc-for-noobs--net-10488
http://archive.oreilly.com/pub/a/php/archive/mvc-intro.html
http://www.htmlgoodies.com/beyond/php/article.php/3912211
https://ellislab.com/codeIgniter/user-guide/
http://laravel.com/docs/5.0/routing

https://en.wikipedia.org/wiki/Model–view–controller
https://en.wikipedia.org/wiki/PARC_(company)
https://en.wikipedia.org/wiki/Trygve_Reenskaug

http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)
http://en.wikipedia.org/wiki/Design_pattern

https://www.iconfinder.com/iconsets/Hand_Drawn_Web_Icon_Set

http://strut.io/
References

Use a spacebar or arrow keys to navigate