This is a simple MVC framework for building web applications in PHP. It's free and open-source.
- First, download the framework, either directly or by cloning the repository.
- Run
composer install
to install the project dependencies. - Configure your web server to have the public folder as the web root.
- Create routes, add controllers, views and models.
Routes are added with the add method. You can add fixed URL routes, and specify the controller and action, like this:
$app->router->get('/', [Controller::class, 'action']);
Or you can add route without controller and return view, like this:
$app->router->get('/', 'view');
Controllers respond to user actions (clicking on a link, submitting a form etc.). Controllers are classes that extend the App\Http\Controllers\Controller
class.
Controllers are stored in the App\Http\Controllers
folder. A sample index controller and login controller included. Controller classes need to be in the App\Http\Controllers
namespace. You can add subdirectories to organise your controllers, so when adding a route for these controllers you need to specify the namespace.
Views are used to display information (normally HTML). View files go in the templates folder. Views can be in one format: .blade.php. No database access or anything like that should occur in a view file. You can render a standard blade.php view in a controller, optionally passing in variables, like this:
use Core\View;
new View('index', [
'name' => 'Dave',
'colours' => ['red', 'green', 'blue']
]);
Models are used to get and store data in your application. They know nothing about how this data is to be presented in the views. Models extend the Core\Concerns\Model
class and use PDO to access the database. They're stored in the App/Models
folder. A sample user model class is included in App/Models/User.php
User::get();
User::where('name', 'John')->get();
User::where('name', 'John')->first();
User::where('name', 'John')->update($data);
or
User::where('name', 'John')->first()->update($data);
User::create($data);
Validator are used to validate request in controllers. You need to pass data to validator, like this:
use Core\Validator;
Validator::make($data, $rules, $messages, $attributes)->validate();
You can also use controller function validate()
. A simple validation is included in App/Http/Controllers/Auth/LoginController.php
After validation you can get errors with Core\Error\Error
class, like this:
use Core\Error\Error;
Error::get($field);
Error::all(); //return all error
Error::has($field); //return boolean
Error::first($field); //return first eror for field
Error::last($field); //return last eror for field
Also you can use $errors
variable in views. A simple usage is included in templates/auth/login.blade.php