Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
From Spaghetti to MVC
Hanafiah Yahya
Who is Hanafiah Yahya?
● Muhamad Hanafiah Bin Yahya
● Digital Immigrants generations
● Started programming, 1997 - MIRC sc...
Our Awesome Programs
http://www.kreydle.com/academy/
Kreydle Academy
● CSR - Corporate social responsibility
● Non profit initiative…
● We contribute back to community
● Helpi...
Kreydle Academy
● Want to contribute?
● Help us spread the word. share with your
friends on twitter, facebook, blog and an...
http://www.kreydle.com/internship/
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
Survey
1. Home many students/fresh graduate here?
2. Code in PHP?
3. Used MVC?
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
http://www.venicecafechicago.com/wp-content/uploads/2014/07/venicea01.jpg
Spaghetti
Why Spaghetti? - metaphor
Things get really messy when connecting each
other
back to the invention of
http://i144.photobucket.com/albums/r166/Funnyoldlife/tin-can-telephone.jpg
A
B
Spaghetti
A
B
C
Spaghetti
A
B
C
D
E
F
Spaghetti
A
B
C
D
E
F
G
H
I
J
Spaghetti
A
B
C
D
E
F
G
H
I
J
Formula
# Phones # Lines/ Connections
2 1
3 3
4 6
5 10
10 45
20 190
1000 ?
Formula
# Phones # Lines
10 45
20 190
1000 499,500
Formula
#Lines = ( p * ( p - 1 ) ) / 2
*p = phone
Where is my server?
Spaghetti Code
1) Spaghetti code is a specific [ goto 8 ]
2) written in order, but splattered [ goto 9 ]
3) allowed to jum...
Solution?
Solution? better design pattern
HUB
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
...
MVC
Heroes
http://www.helenbrowngroup.com/wp-content/uploads/2014/09/info-pro-as-superman.jpg
Enough Theory!
Lets see some code
SQL Query
DB Connection
SQL Query
HTML Code
Business Logic
Presentation
Presentation
Spaghetti Code
http://masterphp.local/spaghetti/
https://github.
com/hanafiah/masterphp/tree/master/spaghetti
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
History
Invented in 1979 by Smalltalk programmers,
Trygve Reenskaug
It is software design pattern
http://heim.ifi.uio.no/~...
History
1979?.. so old
Made popular by Ruby On Rails
initial release on 2005 and
adapted by PHP community
https://upload.w...
Why MVC?
● Organized & structured
● Separation of code
● loose coupling
● Easy code maintenance
● Reusable code. DRY - Don...
Model
View Controller
User
Uses
ManipulateUpdates
Sees
Model
View Controller
User
Uses
ManipulateUpdates
Sees
- Represent Knowledge
- Manages data, logic and rules of the applic...
Model
View Controller
User
Uses
ManipulateUpdates
Sees
- The output, whats user see.
- Visual represent of the Model
- Pre...
Model
View Controller
User
Uses
ManipulateUpdates
Sees
the middleman / link between user and
the system
Model
View Controller
User
Uses
ManipulateUpdates
Sees
The flow
Model
View Controller
User
Uses
ManipulateUpdates
Sees
Model
View Controller
User
Uses
ManipulateUpdates
Sees
Model
View Controller
User
Uses
ManipulateUpdates
Sees
Model
View Controller
User
Uses
ManipulateUpdates
Sees
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
http://www.makemark.co.uk/wp-content/uploads/2011/05/infooverload_full.jpg
PHP MVC Frameworks
1. CodeIgniter
2. Laravel
3. Symfony
4. CakePHP
5. FuelPHP
6. YII
7. … more
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
Why CodeIgniter
Why not CakePHP or Laravel or Symfony or
FuelPHP or YII or Zend Framework or Kohana
or Zoop or Akelos or P...
Why CodeIgniter
Because you’ve gotta pick one
That being said, CI is
not dead!
● easy to understand
● simple, doesn't require advance OOP
● quick to get up and running
...
CodeIgniter is a powerful PHP framework with
a very small footprint, built for developers who
need a simple and elegant to...
Model
View Controller
User
Uses
ManipulateUpdates
Sees
Model
View Controller
User
Uses
ManipulateUpdates
Sees
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
Config - database
application/config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => ‘mas...
Config - autoload
application/config/autoload.php
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
Model
View Controller
User
Uses
ManipulateUpdates
Sees
http://www.codeigniter.com/user_guide/general/models.html
Models
application/models/m_users.php
<?php
public function get_users()
{
$query = $this->db->get('users');
return $query->result();
}
<?php
// Get all users
$s...
<?php
public function insert()
{
$this->name = $this->input->post('name'); //
similar to $_POST['name'];
$this->age = $thi...
<?php
public function update()
{
$this->name = $this->input->post('name'); //
similar to $_POST['name'];
$this->age = $thi...
<?php
public function delete($id = FALSE)
{
$this->db->delete('users', array('id' => $id));
}
<?php
$stmt = $dbh->prepare(...
Model
github repo
https://github.
com/hanafiah/masterphp/blob/master/mvc/application/mod
els/m_users.php
https://github.
c...
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
http://www.makemark.co.uk/wp-content/uploads/2011/05/infooverload_full.jpg
Model
View Controller
User
Uses
ManipulateUpdates
Sees
http://www.codeigniter.com/user_guide/general/views.html
3 View files
application/views/*
<?php
foreach ($users as $user) {
?>
<tr>
<td><?php echo $user->id; ?></td>
<td><?php echo $user->name; ?></td>
<td><?php ...
View
github repo
https://github.
com/hanafiah/masterphp/tree/master/mvc/application/views
https://github.
com/hanafiah/mas...
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
Model
View Controller
User
Uses
ManipulateUpdates
Sees
http://www.codeigniter.com/user_guide/general/controllers.html
Controller
A Controller is simply a class file that is named
in a way that can be associated with a URI.
Relation with url
http://example.com/index.php/CLASS/METHOD/PARAM
http://masterphp.local/mvc/index.php/users/edit/2
<?php
...
Relation with url
http://example.com/index.php/CLASS/METHOD/PARAM
http://masterphp.local/mvc/index.php/users/edit/2
<?php
...
Relation with url
http://example.com/index.php/CLASS/METHOD/PARAM
http://masterphp.local/mvc/index.php/users/edit/2
<?php
...
Relation with url
http://example.com/index.php/CLASS/METHOD/PARAM1/PARAM2
http://masterphp.local/mvc/index.php/users/edit/...
application/controllers/*
Controller
git repo
https://github.
com/hanafiah/masterphp/blob/master/mvc/application/contr
ollers/users.php
Topics
1. Spaghetti Code
2. MVC introduction
3. PHP MVC frameworks
4. Working with CodeIgniter
5. Config
6. Model
7. View
...
pretty url
http://masterphp.local/mvc/index.php/users/edit/2
http://masterphp.local/mvc/users/edit/2
1. add .htaccess to r...
pretty url
http://masterphp.local/mvc/index.php/users/edit/2
http://masterphp.local/mvc/users/edit/2
2. edit config file a...
Continue your journey
1. Library - the classes
2. Helpers - set of functions eg: url, {
base_url(), site_url(), anchor() }...
http://suttonschoolswork.co.uk/wp-content/uploads/2014/10/questionmark.jpg
https://www.facebook.com/kreydle
Thank You
Slide
https://goo.gl/dzfn0D
repo
https://github.com/hanafiah/masterphp
https://www.facebook.com/hanafiah.yahya
h...
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC Frameworks
Upcoming SlideShare
Loading in …5
×

of

YouTube videos are no longer supported on SlideShare

View original on YouTube

Introduction to Using PHP & MVC Frameworks Slide 2 Introduction to Using PHP & MVC Frameworks Slide 3 Introduction to Using PHP & MVC Frameworks Slide 4 Introduction to Using PHP & MVC Frameworks Slide 5 Introduction to Using PHP & MVC Frameworks Slide 6 Introduction to Using PHP & MVC Frameworks Slide 7 Introduction to Using PHP & MVC Frameworks Slide 8 Introduction to Using PHP & MVC Frameworks Slide 9 Introduction to Using PHP & MVC Frameworks Slide 10 Introduction to Using PHP & MVC Frameworks Slide 11 Introduction to Using PHP & MVC Frameworks Slide 12 Introduction to Using PHP & MVC Frameworks Slide 13 Introduction to Using PHP & MVC Frameworks Slide 14 Introduction to Using PHP & MVC Frameworks Slide 15 Introduction to Using PHP & MVC Frameworks Slide 16 Introduction to Using PHP & MVC Frameworks Slide 17 Introduction to Using PHP & MVC Frameworks Slide 18 Introduction to Using PHP & MVC Frameworks Slide 19 Introduction to Using PHP & MVC Frameworks Slide 20 Introduction to Using PHP & MVC Frameworks Slide 21 Introduction to Using PHP & MVC Frameworks Slide 22 Introduction to Using PHP & MVC Frameworks Slide 23 Introduction to Using PHP & MVC Frameworks Slide 24 Introduction to Using PHP & MVC Frameworks Slide 25 Introduction to Using PHP & MVC Frameworks Slide 26 Introduction to Using PHP & MVC Frameworks Slide 27 Introduction to Using PHP & MVC Frameworks Slide 28 Introduction to Using PHP & MVC Frameworks Slide 29 Introduction to Using PHP & MVC Frameworks Slide 30 Introduction to Using PHP & MVC Frameworks Slide 31 Introduction to Using PHP & MVC Frameworks Slide 32 Introduction to Using PHP & MVC Frameworks Slide 33 Introduction to Using PHP & MVC Frameworks Slide 34 Introduction to Using PHP & MVC Frameworks Slide 35 Introduction to Using PHP & MVC Frameworks Slide 36 Introduction to Using PHP & MVC Frameworks Slide 37 Introduction to Using PHP & MVC Frameworks Slide 38 Introduction to Using PHP & MVC Frameworks Slide 39 Introduction to Using PHP & MVC Frameworks Slide 40 Introduction to Using PHP & MVC Frameworks Slide 41 Introduction to Using PHP & MVC Frameworks Slide 42 Introduction to Using PHP & MVC Frameworks Slide 43 Introduction to Using PHP & MVC Frameworks Slide 44 Introduction to Using PHP & MVC Frameworks Slide 45 Introduction to Using PHP & MVC Frameworks Slide 46 Introduction to Using PHP & MVC Frameworks Slide 47 Introduction to Using PHP & MVC Frameworks Slide 48 Introduction to Using PHP & MVC Frameworks Slide 49 Introduction to Using PHP & MVC Frameworks Slide 50 Introduction to Using PHP & MVC Frameworks Slide 51 Introduction to Using PHP & MVC Frameworks Slide 52 Introduction to Using PHP & MVC Frameworks Slide 53 Introduction to Using PHP & MVC Frameworks Slide 54 Introduction to Using PHP & MVC Frameworks Slide 55 Introduction to Using PHP & MVC Frameworks Slide 56 Introduction to Using PHP & MVC Frameworks Slide 57 Introduction to Using PHP & MVC Frameworks Slide 58 Introduction to Using PHP & MVC Frameworks Slide 59 Introduction to Using PHP & MVC Frameworks Slide 60 Introduction to Using PHP & MVC Frameworks Slide 61 Introduction to Using PHP & MVC Frameworks Slide 62 Introduction to Using PHP & MVC Frameworks Slide 63 Introduction to Using PHP & MVC Frameworks Slide 64 Introduction to Using PHP & MVC Frameworks Slide 65 Introduction to Using PHP & MVC Frameworks Slide 66 Introduction to Using PHP & MVC Frameworks Slide 67 Introduction to Using PHP & MVC Frameworks Slide 68 Introduction to Using PHP & MVC Frameworks Slide 69 Introduction to Using PHP & MVC Frameworks Slide 70 Introduction to Using PHP & MVC Frameworks Slide 71 Introduction to Using PHP & MVC Frameworks Slide 72 Introduction to Using PHP & MVC Frameworks Slide 73 Introduction to Using PHP & MVC Frameworks Slide 74 Introduction to Using PHP & MVC Frameworks Slide 75 Introduction to Using PHP & MVC Frameworks Slide 76 Introduction to Using PHP & MVC Frameworks Slide 77 Introduction to Using PHP & MVC Frameworks Slide 78 Introduction to Using PHP & MVC Frameworks Slide 79 Introduction to Using PHP & MVC Frameworks Slide 80 Introduction to Using PHP & MVC Frameworks Slide 81 Introduction to Using PHP & MVC Frameworks Slide 82 Introduction to Using PHP & MVC Frameworks Slide 83 Introduction to Using PHP & MVC Frameworks Slide 84 Introduction to Using PHP & MVC Frameworks Slide 85 Introduction to Using PHP & MVC Frameworks Slide 86 Introduction to Using PHP & MVC Frameworks Slide 87 Introduction to Using PHP & MVC Frameworks Slide 88 Introduction to Using PHP & MVC Frameworks Slide 89 Introduction to Using PHP & MVC Frameworks Slide 90
Upcoming SlideShare
Spaghetti Code vs MVC
Next
Download to read offline and view in fullscreen.

5 Likes

Share

Download to read offline

Introduction to Using PHP & MVC Frameworks

Download to read offline

Want to get to the next level in PHP? It's time to get more structured, organized and production with MVC frameworks.

Related Books

Free with a 30 day trial from Scribd

See all

Introduction to Using PHP & MVC Frameworks

  1. 1. From Spaghetti to MVC Hanafiah Yahya
  2. 2. 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. 3. Our Awesome Programs
  4. 4. http://www.kreydle.com/academy/
  5. 5. 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. 6. 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. 7. http://www.kreydle.com/internship/
  8. 8. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  9. 9. Survey 1. Home many students/fresh graduate here? 2. Code in PHP? 3. Used MVC?
  10. 10. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  11. 11. http://www.venicecafechicago.com/wp-content/uploads/2014/07/venicea01.jpg Spaghetti
  12. 12. Why Spaghetti? - metaphor Things get really messy when connecting each other
  13. 13. back to the invention of http://i144.photobucket.com/albums/r166/Funnyoldlife/tin-can-telephone.jpg A B
  14. 14. Spaghetti A B C
  15. 15. Spaghetti A B C D E F
  16. 16. Spaghetti A B C D E F G H I J
  17. 17. Spaghetti A B C D E F G H I J
  18. 18. Formula # Phones # Lines/ Connections 2 1 3 3 4 6 5 10 10 45 20 190 1000 ?
  19. 19. Formula # Phones # Lines 10 45 20 190 1000 499,500 Formula #Lines = ( p * ( p - 1 ) ) / 2 *p = phone
  20. 20. Where is my server?
  21. 21. 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. 22. Solution?
  23. 23. Solution? better design pattern HUB
  24. 24. 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. 25. MVC Heroes http://www.helenbrowngroup.com/wp-content/uploads/2014/09/info-pro-as-superman.jpg
  26. 26. Enough Theory! Lets see some code
  27. 27. SQL Query DB Connection
  28. 28. SQL Query HTML Code
  29. 29. Business Logic Presentation Presentation
  30. 30. Spaghetti Code http://masterphp.local/spaghetti/ https://github. com/hanafiah/masterphp/tree/master/spaghetti
  31. 31. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  32. 32. History Invented in 1979 by Smalltalk programmers, Trygve Reenskaug It is software design pattern http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html
  33. 33. History 1979?.. so old Made popular by Ruby On Rails initial release on 2005 and adapted by PHP community https://upload.wikimedia.org/...n_Rails.svg.png
  34. 34. Why MVC? ● Organized & structured ● Separation of code ● loose coupling ● Easy code maintenance ● Reusable code. DRY - Don’t Repeat Yourself
  35. 35. Model View Controller User Uses ManipulateUpdates Sees
  36. 36. Model View Controller User Uses ManipulateUpdates Sees - Represent Knowledge - Manages data, logic and rules of the application. - Business Logic
  37. 37. Model View Controller User Uses ManipulateUpdates Sees - The output, whats user see. - Visual represent of the Model - Presentation layer
  38. 38. Model View Controller User Uses ManipulateUpdates Sees the middleman / link between user and the system
  39. 39. Model View Controller User Uses ManipulateUpdates Sees The flow
  40. 40. Model View Controller User Uses ManipulateUpdates Sees
  41. 41. Model View Controller User Uses ManipulateUpdates Sees
  42. 42. Model View Controller User Uses ManipulateUpdates Sees
  43. 43. Model View Controller User Uses ManipulateUpdates Sees
  44. 44. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  45. 45. http://www.makemark.co.uk/wp-content/uploads/2011/05/infooverload_full.jpg
  46. 46. PHP MVC Frameworks 1. CodeIgniter 2. Laravel 3. Symfony 4. CakePHP 5. FuelPHP 6. YII 7. … more
  47. 47. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  48. 48. 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 …?
  49. 49. Why CodeIgniter Because you’ve gotta pick one
  50. 50. That being said, CI is not dead! ● easy to understand ● simple, doesn't require advance OOP ● quick to get up and running ● good docs
  51. 51. 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. CodeIgniter http://www.codeigniter.com
  52. 52. Model View Controller User Uses ManipulateUpdates Sees
  53. 53. Model View Controller User Uses ManipulateUpdates Sees
  54. 54. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  55. 55. Config - database application/config/database.php
  56. 56. $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 ); http://www.codeigniter.com/user_guide/database/configuration.html <?php $host = 'localhost'; $dbname = 'master_php'; $user = 'root'; $pass = ''; try { // Connecting to MySQL $dbh = new PDO("mysql: host=$host;dbname=$dbname", $user, $pass);
  57. 57. Config - autoload application/config/autoload.php
  58. 58. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  59. 59. Model View Controller User Uses ManipulateUpdates Sees http://www.codeigniter.com/user_guide/general/models.html
  60. 60. Models application/models/m_users.php
  61. 61. <?php public 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()) { Model - Get all users
  62. 62. <?php public 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(); Model - Add new user
  63. 63. <?php public 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(); Model - Edit user
  64. 64. <?php public 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(); Model - Delete user
  65. 65. Model github repo https://github. com/hanafiah/masterphp/blob/master/mvc/application/mod els/m_users.php https://github. com/hanafiah/masterphp/tree/master/spaghetti
  66. 66. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  67. 67. http://www.makemark.co.uk/wp-content/uploads/2011/05/infooverload_full.jpg
  68. 68. Model View Controller User Uses ManipulateUpdates Sees http://www.codeigniter.com/user_guide/general/views.html
  69. 69. 3 View files application/views/*
  70. 70. <?php foreach ($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> ... Views - listing
  71. 71. View github repo https://github. com/hanafiah/masterphp/tree/master/mvc/application/views https://github. com/hanafiah/masterphp/tree/master/spaghetti
  72. 72. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  73. 73. Model View Controller User Uses ManipulateUpdates Sees http://www.codeigniter.com/user_guide/general/controllers.html
  74. 74. Controller A Controller is simply a class file that is named in a way that can be associated with a URI.
  75. 75. 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); }
  76. 76. 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); }
  77. 77. 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. 78. 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); }
  79. 79. application/controllers/*
  80. 80. Controller git repo https://github. com/hanafiah/masterphp/blob/master/mvc/application/contr ollers/users.php
  81. 81. Topics 1. Spaghetti Code 2. MVC introduction 3. PHP MVC frameworks 4. Working with CodeIgniter 5. Config 6. Model 7. View 8. Controller 9. Continue your journey
  82. 82. pretty url http://masterphp.local/mvc/index.php/users/edit/2 http://masterphp.local/mvc/users/edit/2 1. add .htaccess to root directory /applications /index.php /.htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
  83. 83. 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'] = '';
  84. 84. Continue your journey 1. Library - the classes 2. Helpers - set of functions eg: url, { base_url(), site_url(), anchor() } 3. Database Classes & Helper 4. Custom URL Routing 5. more..
  85. 85. http://suttonschoolswork.co.uk/wp-content/uploads/2014/10/questionmark.jpg
  86. 86. https://www.facebook.com/kreydle
  87. 87. Thank You Slide https://goo.gl/dzfn0D repo https://github.com/hanafiah/masterphp https://www.facebook.com/hanafiah.yahya hanafiah@kreydle.com
  • mustafayilmaz28

    May. 15, 2017
  • tienthanhtk11

    Apr. 1, 2016
  • fahimahAKU

    Jan. 5, 2016
  • od3n

    Aug. 28, 2015
  • noktah

    Aug. 27, 2015

Want to get to the next level in PHP? It's time to get more structured, organized and production with MVC frameworks.

Views

Total views

1,974

On Slideshare

0

From embeds

0

Number of embeds

3

Actions

Downloads

53

Shares

0

Comments

0

Likes

5

×