PHP Classes

How to Learn from an Application Development From a Simple PHP MVC Simple Example Package Aula MVC: Simple application using the MVC design patttern

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-11-09 (1 hour ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
aula_mvc 1.0MIT/X Consortium ...5PHP 5, Design Patterns, Applications
Description 

Author

This package provides a simple application using the MVC design pattern.

It provides a controller class that performs actions on user records managed by a model class.

The example script checks the current request method and URI to call the control to perform the actions associated with request values.

Picture of Rodrigo Faustino
  Performance   Level  
Name: Rodrigo Faustino <contact>
Classes: 31 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 20x

Winner: 2x

Example

<?php
namespace Professor\Aula;
use
Professor\Aula\Controller\UserController;

require_once
'../vendor/autoload.php';

$method = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

switch(
$method){
    case
'GET':
        switch(
$uri){
            case
'/users':
               
$users = new UserController();
               
$resposta = $users->getUsers();
                if(
$resposta){
                   
http_response_code(200);
                    echo
json_encode(
                        [
'status'=> true, 'message'=> 'Recebido com sucesso','Usuarios'=> $resposta]
                    );
                }else{
                   
http_response_code(204);
                    echo
json_encode(
                        [
'status'=> false, 'message'=> 'Recebido com sucesso','Usuarios'=> []]
                    );
                }
               
                break;
            case
'/produtos':
               
http_response_code(200);
                echo
json_encode(['status'=> true, 'message'=> 'Recebido com sucesso', 'uri'=> $uri]);
                break;
            default:
                echo
json_encode(["URI invalido"]);
        }
    break;
    case
'POST':
        switch(
$uri){
            case
'/users':
               
$data = json_decode(file_get_contents('php://input'), true);
               
               
$users = new UserController();
               
$resposta = $users->insertUsers($data);
               
http_response_code(200);
                echo
json_encode(['status'=> true, 'message'=> 'Recebido com sucesso','dados'=> $data]);
                break;
            case
'/produtos':
               
$data = json_decode(file_get_contents('php://input'), true);
               
http_response_code(200);
                echo
json_encode(['status'=> true, 'message'=> 'Recebido com sucesso', 'dados'=> $data]);
                break;
            default:
                echo
json_encode(["URI invalido"]);
        }
    break;
    case
'PUT':
        case
'/produtos':
            if(
preg_match('/\/users\/(\d+)/', $uri, $match)){
               
$id = $match[1];
               
$data = json_decode(file_get_contents('php://input'), true);
               
http_response_code(200);
                echo
json_encode(['status'=> true, 'message'=> 'Recebido com sucesso', 'id'=> $id]);
                break;
            }
    break;
    case
'DELETE':
        case
'/produtos':
            if(
preg_match('/\/users\/(\d+)/', $uri, $match)){
               
var_dump($match);exit;
               
$id = $match[1];
               
$data = json_decode(file_get_contents('php://input'), true);
               
http_response_code(200);
                echo
json_encode(['status'=> true, 'message'=> 'Recebido com sucesso', 'id'=> $id]);
                break;
            }
    break;
    default:
    echo
json_encode(["Método invalido"]);
}


  Files folder image Files (4)  
File Role Description
Files folder imagesrc (1 file, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data

  Files folder image Files (4)  /  src  
File Role Description
Files folder imageController (1 file)
Files folder imageModel (1 file)
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (4)  /  src  /  Controller  
File Role Description
  Plain text file UserController.php Class Class source

  Files folder image Files (4)  /  src  /  Model  
File Role Description
  Plain text file User.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0