Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 97 | All time: 9,848 This week: 62 |
Version | License | PHP version | Categories | |||
simple-phpmvc 1.0.0 | MIT/X Consortium ... | 7.1 | Libraries, Design Patterns, PHP 7, Tr... |
Description | Author | |
This package can route requests to controller classes or closures. |
<?php |
A super fast, customizable and lightweight PHP MVC Starter Framework to extend for your own...
Clone this repo -
git clone https://github.com/ManiruzzamanAkash/phpmvc.git
.env
fileDuplicate .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
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']);
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)
{
}
}
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'));
}
}
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.
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. -->
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'); ?>
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');
<!-- 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>
You're welcomed to any open-source contribution under MIT licence.
Create a Pull Request at https://github.com/ManiruzzamanAkash/phpmvc/pulls
Files (22) |
File | Role | Description | ||
---|---|---|---|---|
app (3 directories) | ||||
assets (2 directories) | ||||
database (1 file) | ||||
routes (1 file) | ||||
views (1 file, 2 directories) | ||||
.env.example | Data | Auxiliary data | ||
.htaccess | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
index.php | Example | Example script | ||
README.md | Doc. | Read me |
Files (22) | / | app | / | Base |
File | Role | Description |
---|---|---|
Controller.php | Class | Class source |
functions.php | Example | Example script |
Model.php | Class | Class source |
Router.php | Class | Class source |
Files (22) | / | app | / | Controllers |
File | Role | Description |
---|---|---|
PortfoliosController.php | Class | Class source |
WelcomeController.php | Class | Class source |
Files (22) | / | views |
File | Role | Description | ||
---|---|---|---|---|
partials (3 files) | ||||
portfolios (1 file) | ||||
index.php | Aux. | Auxiliary script |
Files (22) | / | views | / | partials |
File | Role | Description |
---|---|---|
footer.php | Aux. | Auxiliary script |
header.php | Aux. | Auxiliary script |
nav.php | Aux. | Auxiliary script |
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 |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.