PHP MVC SERIES: How to use MVC Structured Codebase in PHP (PART 2)

in #utopian-io8 years ago (edited)

What Will I Learn?

  • You will learn how to create an MVC structured codebase in php

Requirements

  • WAMP/MAMP/LAMP/XAMP or any other PHP/MYSQL STACK

Difficulty

  • Intermediate

Tutorial Contents

This tutorial is a continuation of the previous tutorial , in the last tutorial , we prepared the MODEL , VIEWS and CONTROLLER aspect of the MVC structure. In this tutorial, we will be connecting the MODEL to the VIEWS via the CONTROLLER.

STEP 1: FILL IN THE INDEX FILE

In our last tutorial ,we created an empty index.php, in this tutorial , we will be filling this file up now. Note that this file will be treating and receiving any url request sent into the application.

Below are the codes to be filled in:

<?php
include ('models/logic.php');
  if (isset($_GET['controller']) && isset($_GET['action'])) {
    $controller = $_GET['controller'];
    $action     = $_GET['action'];
  }
  else {
    $controller = 'pages';
    $action     = 'home';
  }
  require_once('views/layout.php');
?>

include ('models/logic.php'); : this includes the logic file which controls how the whole application works

$_GET['controller'] : refers to the controller file that will be used , where $_GET['controller'] will refer to the prefix of the controller file. If you checked our last tutorial, we only have one contoller file, which is pages_controller.php , this shows that the $_GET['controller'] is pages , meaning we might need to create a new controller file if the $_GET['controller'] is expected to be something else.

$_GET['action']: refers to the file that will be served in the function that will be served in PagesController Class, in our last tutorial, we had a home method and an error method , meaning the two actions expected were home and method.

else {$controller = 'pages';$action = 'home';} : This acts as a default controller file and method when $_GET['controller'] and $_GET['action'] are not specified.

require_once('views/layout.php'); : This serves layout.php that was explained in the last tutorial, it creates the layout of the view, layout.php contains <?php require_once('routes.php'); ?> which controls how the VIEWS are being processed.

STEP 2: CREATE & FILL IN THE ROUTE FILE

NOTE: the routes.php file exist in the root folder of the project with the index.php. To make things clear, the root folder will be structured as show in the picture below:

mr.PNG

Now , let's fill in the route.php code:

<?php
  function call($controller, $action) {
    require_once('controllers/' . $controller . '_controller.php');
    switch($controller) {
      case 'pages':
        $controller = new PagesController();
      break;
    }
    $controller->{ $action }();
  }
  $controllers = array('pages' => ['home', 'error']);
  if (array_key_exists($controller, $controllers)) {
    if (in_array($action, $controllers[$controller])) {
      call($controller, $action);
    } else {
      call('pages', 'error');
    }
  } else {
    call('pages', 'error');
  }
?>

If you look through this codes, they are really very self-explanatory. I previously explained how the controller page is being served based on $controller and $action, if you look through this code above properly, you will discover that the function call() helps to refer to the right controller file via $controller variable and also pull the right method via $action.

It's crystal clear that we are done with LOGIC and VIEWS has now being connected via the use of CONTROLLER but to make things clear , I will re-explain two things in a concise form:

  • THE FOLDER STRUCTURE
  • THE REQUEST PATH

THE FOLDER STRUCTURE

  • ROOT FOLDER
    mr.PNG

  • CONTROLLER FOLDER
    c.PNG

  • MODEL FOLDER
    m.PNG

  • VIEWS FOLDER
    v.PNG

THE REQUEST PATH

This is just an explanatory analysis of the request is recieved and treated through out the application.

  • INDEX receives the request and then specifies the Controller and the Action
  • LAYOUT receives the Contoller and action and passes it to ROUTE
  • ROUTE uses the Controler variable to call the right controller and then use the action to call the right method in the controller file
  • Then , the ouput of ROUTE is served in the layout.php and then the request is therefore served approriately.

That's all.

Please, read through the tutorial on emore time starting from the first to get a complete analysis of the MVC structure works

MVC SERIES: How to use MVC Structured Codebase in PHP (PART 1)

Curriculum

PHP MVC SERIES

PHP FORM SERIES

PHP REGEX



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

@akintunde, Contribution to open source project, I like you and upvote.

Wow, I just went through the part 1 and part 2 of this tutorial series, mehn you are good. I am using Laravel now. But this pattern of MVC using PHP itself without any framework looks cool. Man wey sabi don sabi. @akintunde. You be baba for the game.

Lol. Thanks man. I am happy you enjoy it.

Thank you for the contribution. It has been approved.

Suggestions:

  • As an Utopian moderator, I appreciate your contributions. Please keep up the good work!
  • But I have a suggestion for your future tutorials, you should avoid dividing your tutorials into parts. You should not decrease the content per contribution in any post of your series. This is because both it is more fluent to read tutorials one post per one subject and using parts frequently can be seen as a sign of abuse.

You can contact us on Discord.
[utopian-moderator]

Thank you. I will take good note of that.

Hey @akintunde I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.092
BTC 62587.21
ETH 1770.97
USDT 1.00
SBD 0.39