1 of 89

From Spaghetti to MVC

Hanafiah Yahya

2 of 89

Who is Hanafiah Yahya?

  • Muhamad Hanafiah Bin Yahya
  • Digital Immigrants generations
  • Started programming, 1997 - MIRC script, VBA, Pascal,
  • https://my.linkedin.com/in/hanafiahyahya

3 of 89

Our Awesome Programs

4 of 89

5 of 89

Kreydle Academy

  • CSR - Corporate social responsibility
  • Non profit initiative…
  • We contribute back to community
  • Helping Graduates Gain the competitive edge
  • fill in the gap between university and what industry really want
  • Its free for student

6 of 89

Kreydle Academy

  • Want to contribute?
  • Help us spread the word. share with your friends on twitter, facebook, blog and any medium that you have
  • or Join us as guest speaker

http://www.kreydle.com/academy/

7 of 89

8 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

9 of 89

Survey

  • Home many students/fresh graduate here?
  • Code in PHP?
  • Used MVC?

10 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

11 of 89

Spaghetti

http://www.venicecafechicago.com/wp-content/uploads/2014/07/venicea01.jpg

12 of 89

Why Spaghetti? - metaphor

Things get really messy when connecting each other

13 of 89

back to the invention of

http://i144.photobucket.com/albums/r166/Funnyoldlife/tin-can-telephone.jpg

A

B

14 of 89

Spaghetti

A

B

C

15 of 89

Spaghetti

A

B

C

D

E

F

16 of 89

Spaghetti

A

B

C

D

E

F

G

H

I

J

17 of 89

Spaghetti

A

B

C

D

E

F

G

H

I

J

18 of 89

Formula

# Phones

# Lines/ Connections

2

1

3

3

4

6

5

10

10

45

20

190

1000

?

19 of 89

Formula

# Phones

# Lines

10

45

20

190

1000

499,500

Formula

#Lines = ( p * ( p - 1 ) ) / 2

*p = phone

20 of 89

Where is my server?

21 of 89

Spaghetti Code

1) Spaghetti code is a specific [ goto 8 ]

2) written in order, but splattered [ goto 9 ]

3) allowed to jump to a specific [ goto 7 ]

4) the goto statement, which [ goto 3 ]

5) a set of instructions is not [ goto 2 ]

6) spaghetti. It often refers to [ goto 4 ]

7) line of code. [ end ]

8) kind of bad coding, where [ goto 5 ]

9) all over the place ( like a knotted [ goto 6 ] )

22 of 89

Solution?

23 of 89

Solution? better design pattern

HUB

24 of 89

Result

# Phones

# Lines

2

1

3

3

4

6

5

10

10

45

20

190

1000

499,500

# Phones

# Lines

2

2

3

3

4

4

5

5

10

10

20

20

1000

1000

Original Refactor

25 of 89

MVC Heroes

http://www.helenbrowngroup.com/wp-content/uploads/2014/09/info-pro-as-superman.jpg

26 of 89

Enough Theory!

Lets see some code

27 of 89

28 of 89

29 of 89

SQL Query

DB Connection

30 of 89

SQL Query

HTML Code

31 of 89

Business Logic

Presentation

Presentation

32 of 89

Spaghetti Code

33 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

34 of 89

History

Invented in 1979 by Smalltalk programmers, Trygve Reenskaug

It is software design pattern

35 of 89

History

1979?.. so old

Made popular by Ruby On Rails

initial release on 2005 and

adapted by PHP community

36 of 89

Why MVC?

  • Organized & structured
  • Separation of code
  • loose coupling
  • Easy code maintenance
  • Reusable code. DRY - Don’t Repeat Yourself

37 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

38 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

- Represent Knowledge

- Manages data, logic and rules of the application.

- Business Logic

39 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

- The output, whats user see.

- Visual represent of the Model

- Presentation layer

40 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

the middleman / link between user and the system

41 of 89

The flow

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

42 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

43 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

44 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

45 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

46 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

47 of 89

http://www.makemark.co.uk/wp-content/uploads/2011/05/infooverload_full.jpg

48 of 89

PHP MVC Frameworks

  • CodeIgniter
  • Laravel
  • Symfony
  • CakePHP
  • FuelPHP
  • YII
  • … more

49 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

50 of 89

Why CodeIgniter

Why not CakePHP or Laravel or Symfony or FuelPHP or YII or Zend Framework or Kohana or Zoop or Akelos or PHP on Trax or Prado or Seagull or …?

51 of 89

Why CodeIgniter

Because you’ve gotta pick one

52 of 89

That being said, CI is

not dead!

  • easy to understand
  • simple, doesn't require advance OOP
  • quick to get up and running
  • good docs

53 of 89

CodeIgniter

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

54 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

55 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

56 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

57 of 89

Config - database

application/config/database.php

58 of 89

$db['default'] = array(

'dsn' => '',

'hostname' => 'localhost',

'username' => 'root',

'password' => '',

'database' => ‘master_php’,

'dbdriver' => 'mysqli',

'dbprefix' => '',

'pconnect' => FALSE,

'db_debug' => TRUE,

'cache_on' => FALSE,

'cachedir' => '',

'char_set' => 'utf8',

'dbcollat' => 'utf8_general_ci',

'swap_pre' => '',

'encrypt' => FALSE,

'compress' => FALSE,

'stricton' => FALSE,

'failover' => array(),

'save_queries' => TRUE

);

<?php

$host = 'localhost';

$dbname = 'master_php';

$user = 'root';

$pass = '';

try {

// Connecting to MySQL

$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

59 of 89

Config - autoload

application/config/autoload.php

60 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

61 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

62 of 89

Models

application/models/m_users.php

63 of 89

Model - Get all users

<?phppublic function get_users()�{� $query = $this->db->get('users');� return $query->result();�}

<?php// Get all users$stmt = $dbh->prepare("SELECT * FROM users");�$stmt->setFetchMode(PDO::FETCH_ASSOC);�if ($stmt->execute()) {� while ($row = $stmt->fetch()) {

64 of 89

Model - Add new user

<?phppublic function insert()�{� $this->name = $this->input->post('name'); // similar to $_POST['name']; $this->age = $this->input->post('age');� $this->email = $this->input->post('email');�� $this->db->insert('users', $this);�}

<?php$stmt = $dbh->prepare("INSERT INTO users ( name, age, email ) values ( :name, :age, :email )");�$stmt->bindParam(':name', $name, PDO::PARAM_STR);�$stmt->bindParam(':age', $age, PDO::PARAM_INT);�$stmt->bindParam(':email', $email, PDO::PARAM_STR);�$name = $_POST['name'];�$age = $_POST['age'];�$email = $_POST['email'];�$stmt->execute();

65 of 89

Model - Edit user

<?phppublic function update()�{� $this->name = $this->input->post('name'); // similar to $_POST['name']; $this->age = $this->input->post('age');� $this->email = $this->input->post('email');�� $this->db->update('users', $this, array('id' => $this->input->post('id')));�}

<?php$stmt = $dbh->prepare("UPDATE users SET name = :name, age = :age, email = :email WHERE id = :id");�$stmt->bindParam(':name', $name, PDO::PARAM_STR);�$stmt->bindParam(':age', $age, PDO::PARAM_INT);�$stmt->bindParam(':email', $email, PDO::PARAM_STR);�$stmt->bindParam(':id', $id, PDO::PARAM_INT);�$name = $_POST['name'];�$age = $_POST['age'];�$email = $_POST['email'];�$id = $_POST['id'];�$stmt->execute();

66 of 89

Model - Delete user

<?phppublic function delete($id = FALSE)�{ � $this->db->delete('users', array('id' => $id));�}

<?php$stmt = $dbh->prepare("DELETE FROM users WHERE id = :id LIMIT 1");�$stmt->bindParam(':id', $id, PDO::PARAM_STR);��$id = $_GET['delete'];�$stmt->execute();

67 of 89

Model

68 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

69 of 89

http://www.makemark.co.uk/wp-content/uploads/2011/05/infooverload_full.jpg

70 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

71 of 89

3 View files

application/views/*

72 of 89

Views - listing

<?phpforeach ($users as $user) {� ?>� <tr>� <td><?php echo $user->id; ?></td>� <td><?php echo $user->name; ?></td>� <td><?php echo $user->age; ?></td>� <td><?php echo $user->email; ?></td>� <td>� ...

<?php// Get all users$stmt = $dbh->prepare("SELECT * FROM users");�$stmt->setFetchMode(PDO::FETCH_ASSOC);�if ($stmt->execute()) {� while ($row = $stmt->fetch()) {� ?>� <tr>� <td><?php echo $row['id']; ?></td>� <td><?php echo $row['name']; ?></td>� <td><?php echo $row['age']; ?></td>� <td><?php echo $row['email']; ?></td>� <td>

...

73 of 89

View

74 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

75 of 89

Model

View

Controller

User

Uses

Manipulate

Updates

Sees

76 of 89

Controller

A Controller is simply a class file that is named in a way that can be associated with a URI.

77 of 89

Relation with url

http://example.com/index.php/CLASS/METHOD/PARAM

http://masterphp.local/mvc/index.php/users/edit/2

<?php�class Users extends CI_Controller {� public function edit($id=FALSE)� {� if (isset($_POST['btnEdit'])) {� $this->m_users->update();� redirect('users');� }� � $this->view_data['user'] = $this->m_users->get_user($id);� $this->load->view('v_edit_user', $this->view_data);� }

78 of 89

Relation with url

http://example.com/index.php/CLASS/METHOD/PARAM

http://masterphp.local/mvc/index.php/users/edit/2

<?php�class Users extends CI_Controller {� public function edit($id=FALSE)� {� if (isset($_POST['btnEdit'])) {� $this->m_users->update();� redirect('users');� }� � $this->view_data['user'] = $this->m_users->get_user($id);� $this->load->view('v_edit_user', $this->view_data);� }

79 of 89

Relation with url

http://example.com/index.php/CLASS/METHOD/PARAM

http://masterphp.local/mvc/index.php/users/edit/2

<?php�class Users extends CI_Controller {� public function edit($id=FALSE)� {� if (isset($_POST['btnEdit'])) {� $this->m_users->update();� redirect('users');� }� � $this->view_data['user'] = $this->m_users->get_user($id);� $this->load->view('v_edit_user', $this->view_data);� }

80 of 89

Relation with url

http://example.com/index.php/CLASS/METHOD/PARAM1/PARAM2

http://masterphp.local/mvc/index.php/users/edit/2/3

<?php�class Users extends CI_Controller {� public function edit($id=FALSE, $param = ‘’)� {� if (isset($_POST['btnEdit'])) {� $this->m_users->update();� redirect('users');� }� � $this->view_data['user'] = $this->m_users->get_user($id);� $this->load->view('v_edit_user', $this->view_data);� }

81 of 89

application/controllers/*

82 of 89

Controller

83 of 89

Topics

  • Spaghetti Code
  • MVC introduction
  • PHP MVC frameworks
  • Working with CodeIgniter
  • Config
  • Model
  • View
  • Controller
  • Continue your journey

84 of 89

pretty url

http://masterphp.local/mvc/index.php/users/edit/2

http://masterphp.local/mvc/users/edit/2

  • add .htaccess to root directory

/applications

/index.php

/.htaccess

RewriteEngine On�RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php/$1 [L]

85 of 89

pretty url

http://masterphp.local/mvc/index.php/users/edit/2

http://masterphp.local/mvc/users/edit/2

2. edit config file and delete index.php

applications/config/config.php

<?php�$config['index_page'] = 'index.php';��become�$config['index_page'] = '';

86 of 89

Continue your journey

  • Library - the classes
  • Helpers - set of functions eg: url, { base_url(), site_url(), anchor() }
  • Database Classes & Helper
  • Custom URL Routing
  • more..

87 of 89

http://suttonschoolswork.co.uk/wp-content/uploads/2014/10/questionmark.jpg

88 of 89

89 of 89

Thank You