- Requirement
- Installation
- Usage
- Configuration
- Routing
- Controller
- Migration
- Database and Models
- Helper functions
PHP 8.0+
1- Download Zip and extract.
2- Run Migration:
php mil.php3- Run server:
php -S localhost:80- set database configuration on
Core/config.php. - access to configs using
config()function
echo config("db_name"); // php_mvc_frameworkyou can define routes on /Routes.php;
urlfor access url.namefor route name that can access withroute()function.controllerfor controller classmethodfor controller callback method
[
"url" => "/welcome",
"name" => "welcome",
"controller" => Controllers\Welcome::class,
"method" => 'index'
]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
}
}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/showYou can make controllers within /Controllers directory.
Controllers should extend Core/BaseController.php
namespace Controllers;
use Core\BaseController;
class Users extends BaseController
{
// methods
}You can create migration files using cli commands
start cli file by opening mil.php file
php mil.phpthen you can create migration file by commands and migrations will be created in Database directory.
migrations should have 'name' and 'columns' keys, which 'columns' should be an array of columns.
[
"name" => "users",
"columns" => [
"id INT AUTO_INCREMENT PRIMARY KEY",
"name VARCHAR(255) NOT NULL",
]
]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);loads a view file from View:
view('dashboard/profile'); // require View/dashboard/profile.phpyou 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 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 ]);it returns string of file and public dir that defined in config.php:
echo public_dir('style.css'); // http://localhost/public/style.cssthis function abort execution with given status code.
you can define view for it in Views/errors.
it will die and dump what ever you give it.
dd("hi"); // string(2) "hi"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.