A quick intro to mvc
mvc in a nutshell
CODE REUSABILITY
SEPARATION OF CONCERNS
- MODEL - The 'things' in the program containing all of the business rules, logic, and functions. (User, Dataset)
- VIEW - The user interface, or point of interaction with the program.
-
Controller - Mediates input and dispatches commands to the model or view.
all together
the model
- Models are the 'things' in our programs
- Often represented by classes or class-like objects
- Often persisted to a database
class User extends Eloquent {
protected $table = 'users';
... more User code ... }
models continued
The ORM
Most MVC frameworks provide an ORM, or Object Relational Mapper to make storing and retrieving models in the database easier.
Codeigniter has no built-in ORM so......
We use 'Eloquent' to do things like
$users = User::all();
$user = User::find(1);
the view
AKA User Interface, UI, GUI, Front-end, HCI (human computer interface) among others.
Just HTML + Mix of PHP
<html> <body> <h1>Hello, <?php echo $user->name; ?>
</h1> </body> </html>
Advanced view
codeigniter (plain php)
<h1><?php echo $heading;?></h1>
<h3>My Todo List</h3>
<ul>
<?php foreach ($todo_list as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
Advanced view
Laravel
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
@while (true)
<p>I'm looping forever.</p>
@endwhile
SO
Models hold the data/logic about our program.
Views show this data to the users of the program.
How do we glue the models and views together?
The Controller
Basically a class file that is named so that it can be accessed by a URL
example: http://website.com/user/name
<?php class User extends My_Controller { public function name() {
// Find user model using Laravel ORM
$user = User::find(1);
// Control some other stuff.
// Respond with view.
} } ?>
Controllers irl
http://mocavo.com/[Class.php]/[#function]
/order
order.php -> Order class -> #index
/auth/logout
auth.php -> Auth class -> #logout
/order/thank_you
order.php -> Order class -> #thank_you
codeigniter controller/view
website.com/user
<!-- application/controllers/user.php -->
Class User extends My_Controller {
public function index() {
$data = array($name => 'darrell');
$this->load->view('greeting', $data);
}
}
<!-- application/views/greeting.php --> <html> <body> <h1>Hello, <?= $name; ?></h1> </body> </html>
Laravel Controller/View
website.com/user
<!-- app/controllers/greeting.php -->
Route::get('/user', function() {
$data = array($name => 'Darrell');
return View::make('greeting', $data); });
<!-- app/views/greeting.php --> <html> <body> <h1>Hello, <?php echo $name; ?></h1> </body> </html>
Questions?
A Quick Intro To MVC
By hacknightly
A Quick Intro To MVC
- 57