PHP Classes

Basic PHP MVC Basic Model: Implement MVC applications with a router class

Recommend this page to a friend!
  Info   View files Example   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-11-13 (3 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 96 All time: 9,817 This week: 100Up
Version License PHP version Categories
php-mvc-basic-model 1.0.0Freeware8.2PHP 5, Libraries, Design Patterns
Description 

Author

Developers can use this package to implement MVC applications with a router class.

It provides a router class that allows developers to associate a given request URL path to a controller class.

Structure

The project is organized into several directories:

router/

- Contains the Router class responsible for handling routing logic.


controllers/

- Holds controller classes, each managing the application's logic for a specific route.

views/

- It contains view classes responsible for rendering HTML content using data obtained from models.

models/

- Holds model classes, dealing with data retrieval and manipulation for each specific view.php

Picture of Adrian M
  Performance   Level  
Name: Adrian M <contact>
Classes: 3 packages by
Country: Ireland Ireland
Age: 43
All time rank: 40545 in Ireland Ireland
Week rank: 25 Up1 in Ireland Ireland Up

Example

<?php
include('router/Router.php');

$router = new Router();

$router->addRoute('/home', 'HomeController');
$router->addRoute('/about', 'AboutController');
$router->addRoute('/contact', 'ContactController');

$url = $_SERVER['REQUEST_URI'];


$router->dispatch($url);


Details

PHP-MVC

Simple PHP MVC Router

This project is a basic demonstration of a PHP MVC (Model-View-Controller) structure implementing a router for managing routes and views. It's designed to showcase a simple MVC pattern for a web application.

Structure

The project is organized into several directories:

router/

Contains the Router class responsible for handling routing logic.

controllers/

Holds controller classes, each managing the application's logic for a specific route.

views/

Contains view classes, responsible for rendering HTML content using data obtained from models.

models/

Holds model classes, dealing with data retrieval and manipulation for each specific view.

Usage

To use the project:

  1. Clone the repository.
  2. Set up your local server to point to the project directory.
  3. Access the application via your web browser.

The entry point is index.php, which initiates the router and dispatches to the appropriate controller based on the URL.

Code Example

HomeController.php (Controller)

class HomeController {
    public function invoke() {
        $model = new HomeModel();
        $view = new HomeView($model);
        $view->render();
    }
}

The HomeController demonstrates how the controller interacts with the model to retrieve data and then passes it to the view for rendering.

HomeView.php (View)

class HomeView {
    private $model;

    public function __construct($model) {
        $this->model = $model;
    }

    public function render() {
        $data = $this->model->getData();
        echo "<h1>$data</h1>";
        // Other HTML content or formatting specific to the view can be added here
    }
}

The HomeView class illustrates the view's responsibility of rendering the HTML content, using data obtained from the model.

This README offers a brief overview of the project's structure, usage instructions, and code snippets to showcase the MVC architecture's components. Feel free to expand on it with installation instructions, dependencies, or any additional features you might have in your project!


  Files folder image Files  
File Role Description
Files folder imagecontrollers (3 files)
Files folder imagemodels (3 files)
Files folder imagerouter (1 file)
Files folder imageviews (3 files)
Accessible without login Plain text file .htaccess Data Auxiliary data
Accessible without login Plain text file FileStructure.php Aux. Auxiliary script
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  controllers  
File Role Description
  Plain text file AboutController.php Class Class source
  Plain text file ContactController.php Class Class source
  Plain text file HomeController.php Class Class source

  Files folder image Files  /  models  
File Role Description
  Plain text file AboutModel.php Class Class source
  Plain text file ContactModel.php Class Class source
  Plain text file HomeModel.php Class Class source

  Files folder image Files  /  router  
File Role Description
  Plain text file Router.php Class Class source

  Files folder image Files  /  views  
File Role Description
  Plain text file AboutView.php Class Class source
  Plain text file ContactView.php Class Class source
  Plain text file HomeView.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:96
This week:0
All time:9,817
This week:100Up