Hướng dẫn what is mbc php?

Introduction

Today I am going to show how to create a simple PHP application following the MVC pattern [Model-View-Controller]. I was inspired by a PHP course I taught some years ago, and in which I built a simple e-commerce with the students. This e-commerce was based on a simple MVC framework based on PHP. Then, people who have continued with code and programming already had a smattering of what means MVC before get their hands on a real framework.

Nội dung chính

  • Introduction
  • What does mean MVC?
  • Build a simple PHP MVC framework
  • The htaccess configuration file
  • Bootstrap your PHP MVC framework
  • The routing system
  • The homepage
  • Improve the PHP MVC framework
  • Download the code
  • Can you use PHP in MVC?
  • What is MVC in PHP w3schools?
  • Why MVC is used in PHP?
  • What is MBC PHP?

MVC frameworks are widely used in the industry because they offer a lot of advantages for rapid and structured development. There are MVC frameworks for most of the programming languages you may know, from DotNet to PHP. Unfortunately, those frameworks might have a steep learning curve. This is due to the fact that people need to learn how to write code in the framework ecosystem.

Personal note: in 2010 I was already developing software for more than 5 years, and I was looking for a good solution in order to build a web application for my boss. Briefing with a former colleague of mine [thanks Davide C.!], I started using Symfony 1.4. I used the “RTFM approach” [Read The Friendly Manual…] before writing any code. In two months I realized a medium-complex application [registration, ACL, dashboard, frontend, etc].

After that, I worked on Zend Framework, Symfony 2.0 and 5, and Laravel [currently working on 5.8], and also on microframeworks like Silex [not maintained anymore] and Lumen. Without any doubt, my favorite framework is Laravel. Despite some “magical things” that can scare people, Laravel offers a lot of out-of-the-box features that you can simply activate with the right configuration setting.

What does mean MVC?

MVC is a design pattern used to decouple data [Models], the user-interfaces [Views], and application logic [Controllers]. To be able to follow this “How to”, you need to have a good knowledge of PHP and OOP [Object Oriented Programming].

Build a simple PHP MVC framework

Independently you are using Docker, XAMPP, or whatever for your development environment, let’s create a simple structure for the simple PHP MVC framework. I use to have a folder called “Solutions” for all my projects, then enter your folder, create a new folder called “simple-php-mvc” and then enter that folder.
Let’s create the basis folders for your MVC:

  1. app
  2. config
  3. public
  4. views
  5. routes

Starting small, let’s create the two most important files of our simple PHP MVC: index.php and htaccess.

The htaccess configuration file

Enter the public folder, and let’s create a file called index.php

Now, at the root level of your project, let’s create a new file called .htaccess
Then open it, and put this code inside the htaccess:


RewriteEngine On

# Stop processing if already in the /public directory
RewriteRule ^public/ - [L]

# Static resources if they exist
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule [.+] public/$1 [L]

# Route all other requests
RewriteRule [.*] public/index.php?route=$1 [L,QSA]

Htaccess is a configuration file for the Apache web server, and the mod_rewrite directive tells to Apache that every request will end up to the index.php located in the folder named public. What does it mean? It means that if you browse //simple-php-mvc/page1, //simple-php-mvc/page2 or //simple-php-mvc/page3, all of them will end up in the index.php under public, that is the entry point of your PHP MVC framework. This is a big advantage because you can now handle your request in one place, understand what resource is requested and provide the right response.
Another thing: using htaccess and drive the traffic under the public folder, the rest of your project’s structure will be hidden to anyone.

This is how your project looks like right now:

Folder structure
app
config
public
    index.php
views
routes
.htaccess

Bootstrap your PHP MVC framework

Now you need a way to bootstrap your app and load the code you need. We already said that index.php under the public folder is the entry point, for that reason we include the necessary files from there.

First of all, we load the config file, here is the content of index.php:

// Load Config
require_once '../config/config.php';

Now we can create a config.php file under the config folder.

Inside the config file, we can store the settings of the framework, for example, we can store the name of our app, the path of the root, and of course, the database connection parameters:

Chủ Đề