/php-mvc-simple-framework

A simple PHP MVC framework that works without composer

Primary LanguagePHPMIT LicenseMIT

Simple PHP MVC Framework (no composer)

Table of Content:


Requirement

PHP 7.4+

Installation

1- Download Zip and extract.

2- Run server:

php -S localhost:80

Usage

Configuration

  • set database configuration on Core/config.php.
  • access to configs using config() function
echo config("db_name"); // php_mvc_framework

Routing

you can define routes on /Routes.php;

  • url for access url.
  • name for route name that can access with route() function.
  • controller for controller class
  • method for controller callback method
[
    "url" => "/welcome",
    "name" => "welcome",
    "controller" => Controllers\Welcome::class,
    "method" => 'index'
]


Route Parameters

you can bind parameter to route using {key}:

[
    "url" => "/user/{id}/show",
    "name" => "show_user",
    "controller" => Controllers\Users::class,
    "method" => 'show'
]

The show() function should accept a parameter with $id name as following:

class Users extends BaseController
{
    public function show($id)
    {
        // code    
    }
}


Getting Route By name

you can get routes by its name:

echo route('welcome'); // "localhost/welcome"

if your route has parameter you should define through an array:

echo route('show_user', ['id' => 2]); // localhost/user/2/show

Controller

You can make controllers within /Controllers directory.

Controllers should extend Core/BaseController.php

namespace Controllers;

use Core\BaseController;

class Users extends BaseController
{
    // methods
}

Database and Models

You can create model files within /Models directory. models should extend Core/Model.php:

namespace Models;

use Core\Model;

class Users extends Model
{
    // methods
}

if you created your model as controller name, you do not need to define model in controller.

you can access it with $this->Database in controller:

class Users extends BaseController
{
    public function show()
    {
        dd($this->Database->GetAllUsers());
    }
}

if you want to use another one you can define it in Controller:

protected string $Model = "ShowUser";

there are four methods for CRUD that you have access in model.

how to select:

 public function getUsers()
 {
    $Query = "SELECT * FROM users WHERE is_active = ? AND id != ?";
    $Data = [
        1,
        5
    ];
    return $this->InsertRow($Query,$Data);
 }

and other methods:

$this->SelectRow($Query,$Data);
$this->UpdateRow($Query,$Data);
$this->DeleteRow($Query,$Data);

Helper functions

view()

loads a view file from View:

view('dashboard/profile'); // require View/dashboard/profile.php

you can pass data to view by compact() inner function.

$Users = ['Milad','Ali']; // Data

view('dashboard/profile',compact('Users'));

// or

view('dashboard/profile',['users'=>$Users])

then you can access data in view:

<?php foreach ($Users as $User): ?>
    <tr>
        <td><?=$User?></td>
    </tr>
<?php endforeach; ?>

redirect()

redirect to route by passing route name or url:

redirect('index'); // redirects to index route

redirect('/dashboard/profile');

if route has parameter, you can pass it as in route function

redirect('show_user', ['id' => 1 ]);

public_dir()

it returns string of file and public dir that defined in config.php:

echo public_dir('style.css'); // http://localhost/public/style.css

abort()

this function abort execution with given status code.

you can define view for it in Views/errors.


dd()

it will die and dump what ever you give it.

dd("hi"); // string(2) "hi"

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

For any issue or feature request, please open an issue.

License

MIT License