Clone repository
git clone https://github.com/RaoulvanWijk/mvc.git
Install composer packages
composer install
To run the framework you have 2 options: Run it from command line
$ php mvc serve
or run it by creating a virtual host.
Note that mvc server uses the PHP server under the hood which is NOT meant to used as a web server so only use mvc serve
for development purposes!
And make sure you are running your database if you want to use it
Go to the web.php file in the routes folder
use App\Http\Route;
use App\Http\Controllers\DemoController;
Route::get('url', [DemoController:class, 'method'], "name");
Route::post('url', [DemoController:class, 'method'], "name");
Route::put('url', [DemoController:class, 'method'], "name");
Route::delete('url', [DemoController:class, 'method'], "name");
Route parameters
Route::get('url/{id}/another/{param}', [DemoController:class, 'method'], "name");
Route::prefix('/demo')->group(function() {
Route::get('url', [DemoController:class, 'method'], "route");
Route::get('url', [DemoController:class, 'method'], "anotherRouteInGroup");
});
Middleware is used to validate the uri request
To create middleware execute the following command:
$ php mvc make:middleware NameOfTheMiddleware
Adding middleware to a single route
Route::get('url', [DemoController:class, 'method'], "anotherRouteInGroup", middleware: "nameOfMiddleware");
Adding middleware to grouped routes
Route::prefix('/demo')->group(function() {
Route::get('/url', [DemoController:class, 'method'], "route");
}, ["middleware" => ["nameOfMiddleware"]]);
To specify new middleware to go app\Http\HttpKernel and add youre middleware class to
private $routeMiddleware = [
"nameOfMiddleware" => \App\Http\Middleware\YoureMiddlewareClass
];
A controller is used to communicate between a view and a model
You can create a controller by using the following command:
$ php mvc make:controller YoureNameOfTheController
class DemoController extends Controller
{
public function demo()
{
// Redirect to different route
$this->redirect('name_of_route');
// Redirect with a message
$this->redirect('name_of_route')->with('key', 'value');
// Go to previous route
$this->back();
}
}
class DemoController extends Controller
{
public function demo()
{
// Filename in resources folder
$this->view('file');
}
}
A model is used to interact with the database
You can create a model by using the following command:
$ php mvc make:model YoureNameOfTheModel
The name you give to the model wil also be parsed to the name of your database table for example:
class User
will by default have users as table name
You can change the name of databaseTable by adding
protected $databaseTable = 'tablename';
Below a list of properties that you can change
/**
* Var used to store the database Table of the model
*/
protected string $databaseTable;
/**
* Var used to keep track of tables primary key
*/
protected string $primaryKey = 'id';
/**
* Var used to store all the columns that are allowed to be inserted
*/
protected array $fillable = [];
This model also has a query builder wich you can use example below
public function Demo()
{
// Using not Raw functions are **NOT SAFE** using this without sanitizing the user input is not recommended
return Progress::select()->where('id = 1')->get();
// using Raw functions are safer since it is using prepared statements with binding
return Progress::select()->whereRaw('id = :id', [':id', 1])->get();
// But you should still always sanitize the user input
}
If you dont want to use the query builder you can also execute sql statements by using:
public function Demo()
{
return Progress::query('SELECT * FROM users');
}
At the end of every builded query you need to use
->get();
to execute the sql statement
In this framework you can use a request class to validate the request data before it gets to the controller and redirects the user back to the previous page with a errors
To create a request class use the following command
$ php mvc make:request NameOfRequestClass
To validate the data you need to use the rules()
function as following:
public function rules()
{
return [
'username' => 'required|string|min:8',
'password' => 'required|string|min:8'
];
}
You can also specify what url the request is gona redirect to if it has failed
$redirect_if_failed = 'name_of_route';
U can use the authorize function to validate if a user is allowed to make the request
if authorize()
doesnt return true it will throw en error
public function authorize()
{
return true;
}