PHP Classes

Simple, Fast, Lightweight PHP MVC Framework: Route requests to controller classes or closures

Recommend this page to a friend!
  Info   View files Example   View files View files (22)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 96 This week: 1All time: 9,834 This week: 560Up
Version License PHP version Categories
simple-phpmvc 1.0.0MIT/X Consortium ...7.1Libraries, Design Patterns, PHP 7, Tr...
Description 

Author

This package can route requests to controller classes or closures.

It provides a router class with functions to register controller class functions or closure parts to call when the request is of a specific type under a given URL pattern.

The package also provides base model and controller classes that applications can extend to implement the actual controllers and models it needs.

Applications should implement views using template PHP scripts. The package provides helper functions to simplify view script creation to return the path of assets such as JavaScript, CSS, and images.

Picture of Maniruzzaman Akash
  Performance   Level  
Name: Maniruzzaman Akash <contact>
Classes: 18 packages by
Country: Bangladesh Bangladesh
Age: 28
All time rank: 330944 in Bangladesh Bangladesh
Week rank: 109 Up3 in Bangladesh Bangladesh Up
Innovation award
Innovation award
Nominee: 7x

Example

<?php

use Pecee\SimpleRouter\SimpleRouter;

// Autoload the vendor
require_once __DIR__ . '/vendor/autoload.php';

// Load from environment variables.
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

define('ROOT', __DIR__);
define('VIEWS', __DIR__ . '/views');
define('ASSET_DIR', __DIR__ . '/assets');
define('BASE_DIR', isset($_ENV['BASE_DIR']) ? $_ENV['BASE_DIR'] : '');
define('URL', $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . BASE_DIR);
define('ASSET_URL', URL . '/assets');

/* Load external routes file */
require_once 'routes/route.php';

SimpleRouter::setDefaultNamespace('\App\Controllers');
// Start the routing
SimpleRouter::start();


Details

PHPMVC

A super fast, customizable and lightweight PHP MVC Starter Framework to extend for your own...

How to Start

Clone this repo -

git clone https://github.com/ManiruzzamanAkash/phpmvc.git

Modify the .env file

Duplicate .env.example and create .env file for your own -

BASE_DIR=
APP_TITLE="Site Title"

DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_NAME=phpmvc
  1. `BASE_DIR`: The root directory of your project if you want to keep your project to any sub-directory of the domain, like `devsenv.com/new-app`. If no sub-directory is required, leave it empty.
  2. `APP_TITLE`: The title of your site.
  3. `DB_HOST`: The hostname of your database server, default : `localhost`.
  4. `DB_PORT`: The port of your database server, default: `3306`
  5. `DB_USER`: The username of your database server, default: `root`.
  6. `DB_PASSWORD`: The password of your database server. default: empty.
  7. `DB_NAME`: The name of your database, default: `phpmvc`.

Extend: Add your own MVC

Route

Example 1: Add routes in route.php

<?php

use App\Base\Router;
use App\Controllers\WelcomeController;

Router::get('/', [WelcomeController::class, 'hello']);

Example 2: Closure function

use App\Base\Router;

Router::get('/', function() {
    return 'Hello World';
});

Router::get('/hello-another', function() {
    return views('welcome/hello');
});

Example 3: Portfolio Route


use App\Base\Router;
use App\Controllers\PortfoliosController;

Router::get('portfolios', [PortfoliosController::class, 'index']);

Model

We can create any model inside app\Models folder by extending base Model class.

Example 1: Create a TestModel class in app\Models\TestModel.php.

<?php

namespace App\Models;

use App\Base\Model;

class TestModel extends Model
{
   //
}

Example 2: Portfolio Model: in app\Models\Portfolio.php

<?php

namespace App\Models;

use App\Base\Model;

class Portfolio extends Model
{
    protected string $tableName = 'portfolios';

    public function get(): array|false
    {
        return $this->fetchAll("SELECT * FROM {$this->tableName}");
    }

    public function findById(int $id)
    {
    }
}

Controller

We can create any controller inside app\Controllers folder by extending base Controller class.

Example 1: TestsController in app\Controllers\TestsController.php

<?php

namespace App\Controllers;

use App\Base\Controller;

class TestsController extends Controller
{
    public function index()
    {
        //
    }
}

Example 2: PortfoliosController in app\Controllers\PortfoliosController.php

<?php

namespace App\Controllers;

use App\Base\Controller;
use App\Models\Portfolio;

class PortfoliosController extends Controller
{
    public function index()
    {
        $portfolio = new Portfolio();
        $portfolios = $portfolio->get();

        return views('portfolios/index.php', compact('portfolios'));
    }
}

Views

We can create any view file inside views folder.

Example 1: Simple view file: in views/index.php

<h2>Home Page</h2>

Example 2: View file with extending header and footer.

View Header:

in views/partials/header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo env('APP_TITLE'); ?></title>

    <!-- Load Styles -->
    <link rel="stylesheet" href="<?php echo assets('css/style.css'); ?>">
</head>
<body>
    <!-- Load Header Nav-->
    <?php views('/partials/nav.php'); ?>

    <!-- Content start -->
    <div class="main-content">
        <!-- Content will be loaded here. -->

View Footer:

in views/partials/footer.php

</div>
<!-- Script -->
<script src="<?php echo assets('js/base.js'); ?>"></script>
</body>
</html>

Portfolio main view by extending header and footer.

<?php views('/partials/header.php'); ?>

<h2>Portfolios</h2>
<?php foreach($portfolios as $portfolio): ?>
    <li><?php echo $portfolio['title']; ?></li>
<?php endforeach;?>

<?php views('/partials/footer.php'); ?>

Helper Methods

views()

You can load any view file inside views folder using this views() function.

// Index view file
views('index.php');

// Portfolio Views
views('portfolios/index.php');

// Pass additional data.
$name = 'Akash';
views('portfolios/index.php', compact('name'));

// Or pass multiple data.
$portfolios = [
    ['title' => 'Portfolio 1'],
    ['title' => 'Portfolio 2'],
];

views('portfolios/index.php', compact('portfolios'));

assets()

Assets will be loaded from assets folder. You load CSS, JS or images by calling assets method.

assets('css/style.css');

assets('js/base.js');

env()

Get environment variables by calling env method.

env('DB_NAME');

env('APP_TITLE');

url()

Create an url by the given path with this url() function.

// Home URL
url('/');

// Portfolios URL
url('portfolios');

Contributors

<!-- Table of contributors --> <table class="table">

<thead>
    <tr>
        <th>Name</th>
        <th>Github</th>
        <th>Email</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Maniruzzaman Akash</td>
        <td><a href="https://github.com/ManiruzzamanAkash">ManiruzzamanAkash</a></td>
        <td>
            <a href="mailto:manirujjamanakash@gmail.com">manirujjamanakash@gmail.com
            </a>
        </td>
    </tr>
</tbody>

</table>

New Contribution

You're welcomed to any open-source contribution under MIT licence.

Create a Pull Request at https://github.com/ManiruzzamanAkash/phpmvc/pulls


  Files folder image Files  
File Role Description
Files folder imageapp (3 directories)
Files folder imageassets (2 directories)
Files folder imagedatabase (1 file)
Files folder imageroutes (1 file)
Files folder imageviews (1 file, 2 directories)
Accessible without login Plain text file .env.example Data Auxiliary data
Accessible without login Plain text file .htaccess Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  app  
File Role Description
Files folder imageBase (4 files)
Files folder imageControllers (2 files)
Files folder imageModels (1 file)

  Files folder image Files  /  app  /  Base  
File Role Description
  Plain text file Controller.php Class Class source
  Accessible without login Plain text file functions.php Example Example script
  Plain text file Model.php Class Class source
  Plain text file Router.php Class Class source

  Files folder image Files  /  app  /  Controllers  
File Role Description
  Plain text file PortfoliosController.php Class Class source
  Plain text file WelcomeController.php Class Class source

  Files folder image Files  /  app  /  Models  
File Role Description
  Plain text file Portfolio.php Class Class source

  Files folder image Files  /  assets  
File Role Description
Files folder imagecss (1 file)
Files folder imagejs (1 file)

  Files folder image Files  /  assets  /  css  
File Role Description
  Accessible without login Plain text file style.css Data Auxiliary data

  Files folder image Files  /  assets  /  js  
File Role Description
  Accessible without login Plain text file base.js Data Auxiliary data

  Files folder image Files  /  database  
File Role Description
  Accessible without login Plain text file phpmvc.sql Data Auxiliary data

  Files folder image Files  /  routes  
File Role Description
  Plain text file route.php Class Class source

  Files folder image Files  /  views  
File Role Description
Files folder imagepartials (3 files)
Files folder imageportfolios (1 file)
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files  /  views  /  partials  
File Role Description
  Accessible without login Plain text file footer.php Aux. Auxiliary script
  Accessible without login Plain text file header.php Aux. Auxiliary script
  Accessible without login Plain text file nav.php Aux. Auxiliary script

  Files folder image Files  /  views  /  portfolios  
File Role Description
  Accessible without login Plain text file index.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:96
This week:1
All time:9,834
This week:560Up