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

Related tags

Frameworks phpmvc
Overview

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

Name Github Email
Maniruzzaman Akash ManiruzzamanAkash [email protected]

New Contribution

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

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

You might also like...
A simple PHP MVC framework without extra files and codes that you don't need

Welcome to (SPM) Simple PHP MVC, just what you need! This is a simple PHP MVC framework without extra files and codes that you don't need.

CleverStyle Framework is simple, scalable, fast and secure full-stack PHP framework

CleverStyle Framework is simple, scalable, fast and secure full-stack PHP framework. It is free, Open Source and is distributed under Free Public Lice

Framework X is a simple and fast micro framework based on PHP
Framework X is a simple and fast micro framework based on PHP

Framework X is a simple and fast micro framework based on PHP. I've created a simple CRUD application to understand how it works. I used twig and I created a custom middleware to handle PUT, DELETE methods.

Sunhill Framework is a simple, fast, and powerful PHP App Development Framework

Sunhill Framework is a simple, fast, and powerful PHP App Development Framework that enables you to develop more modern applications by using MVC (Model - View - Controller) pattern.

This is an attempt at making my own personal PHP framework purely for learning purposes.

PHP-MVC-FRAMEWORK This is an attempt at making my own personal PHP framework purely for learning purposes. Installation Download the archive or clone

a micro mvc framework for php

micro-mvc-php a micro mvc framework for php Config your Web url in .env . lifecycle All request proccess by index.php Autoload files include in bootst

The Hive is a simple php mvc framework
The Hive is a simple php mvc framework

Hive framework The Hive is a simple php mvc framework . Information Features : -MVC design -PDO connection -OOP system -Twig template -Very Fast, simp

Minimal PHP MVC Framework that is eternally broken.

▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ ██ ▄▄▄ █ ▄▄▀█ ▄▄██▄██ ▄▀██▄██ ▄▄▀█ ▄▄▀ ██ ███ █ ▄▄▀█▄▄▀██ ▄█ █ ██ ▄█ ▀▀ █ ██ ██ ▀▀▀ █▄▄▄▄█▄▄▄█▄▄▄█▄▄██▄▄▄█▄██▄█

A Slim PHP MVC framework built just for fun!

Aura Framework A Slim PHP MVC framework built just for fun! en: Note: This repository only contains the core code of the Aura framework. If you want t

Comments
  • Some SimpleRouter routes not working.

    Some SimpleRouter routes not working.

    When using phpmvc with BASE_DIR set a number of routes report 'route not found'

    For example Router::form() Router::match([get, post]) Router::() ->name('myNamedRoute') ....and more

    These do not work because SimpleRouter cannot handle the BASE_DIR. A work around would be to add the BASE_DIR to each of the above routes. For example Router::match(['get', 'post'], BASE_DIR.'/login', 'AuthController@login');

    Also to use named routes you do not want to add BASE_DIR to the route name which is what your url() function does. Work around is to make a new function function namedRoute(?string $name = null, $parameters = null, ?array $getParams = null): Url { return Router::getUrl($name, $parameters, $getParams); } then call, say, redirect(namedRoute('myRouteName');

    namedRoute() is just your url() function without adding BaseDir

    opened by robertkendrick 0
Owner
Maniruzzaman Akash
Software Engineer | PHP, Laravel, React, Redux, WordPress, Typescript. Can develop your system's Frontend with React/Vue and Backend with Laravel/WordPress.
Maniruzzaman Akash
TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs.

TrailLamp Introduction TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs. Installatio

Etorojah Okon 14 Jun 10, 2022
Woski is a fast and simple lightweight PHP Framework for building applications in the realm of the web.

Woski is a simple fast PHP framework for the Realm The Project Installation Clone the repository $ composer create-project clintonnzedimma/woski myApp

Clinton Nzedimma 19 Aug 15, 2022
I made my own simple php framework inspired from laravel framework.

Simple MVC About Since 2019, I started learning the php programming language and have worked on many projects using the php framework. Laravel is one

null 14 Aug 14, 2022
I made my own simple php framework inspired from laravel framework.

Simple MVC About Since 2019, I started learning the php programming language and have worked on many projects using the php framework. Laravel is one

Rizky Alamsyah 14 Aug 14, 2022
A modern, ultra lightweight and rocket fast Content Management System

Redaxscript A modern, ultra lightweight and rocket fast Content Management System for SQLite, MSSQL, MySQL and PostgreSQL. Installation Clone the repo

redaxscript 247 Nov 12, 2022
a framework for WebDevelop based on the mvc structure. The name of this project for Fun because everyone can use it. Completely simple and powerful structure for all your projects

A_A (-.-) ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ |-| █▄─▄▄─█▄─██─▄█─▄▄▄▄█─▄▄▄▄█▄─█─▄█─▄▄▄─██▀▄─██─▄

MasihGhaznavi 7 Jun 29, 2022
Symprowire is a PHP MVC Framework based and built on Symfony, using the ProcessWire CMS as DBAL and Service Provider.

Symprowire - PHP MVC Framework for ProcessWire 3.x Symprowire is a PHP MVC Framework based and built on Symfony using ProcessWire 3.x as DBAL and Serv

Luis Mendez 7 Jan 16, 2022
FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Ruby on Rails.

FlyCubePHP FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Rub

Anton 1 Dec 21, 2021
💡 Mudrock is a MVC PHP framework, which was inspired by the Laravel and CodeIgniter frameworks.

?? Mudrock is a MVC PHP framework, which was inspired by the Laravel and CodeIgniter frameworks

null 3 Nov 17, 2021