PHP code example of roolith / framework
1. Go to this page and download the library: Download roolith/framework library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
roolith / framework example snippets
namespace App\Models;
class User extends Model
{
protected string $table = 'users';
protected array $fillable = ['name', 'email'];
protected array $casts = ['id' => 'int'];
protected function validationRules(): array
{
return [
'name' => \App\Core\Rules::set()->isRequired()->minLength(2),
'email' => \App\Core\Rules::set()->isRequired()->isEmail(),
];
}
}
$model = new User();
$data = $model->filterFillable(Request::all());
$errors = $model->validate($data);
if ($errors !== []) {
return $errors;
}
User::transaction(static function ($db) use ($data): void {
$db->table('users')->insert($data);
});
\App\Core\DatabaseFactory::transaction(static function ($db) {
$db->table('users')->insert(['name' => 'Hadi']);
});
namespace App\Controllers;
use App\Models\User;
class WelcomeController extends Controller
{
public function index()
{
$data = [
'content' => 'Welcome to Roolith framework!',
'title' => 'Roolith Framework',
];
return $this->view('home', $data);
}
public function users()
{
return User::all();
}
public function show($id)
{
return User::orm()->find($id);
}
}
<form action="<?= route('welcome.form')
Request::input('page');
Request::has('page');
Request::all();
Request::only('page');
Request::only(['page', 'other_param']);
Request::except('password');
Request::cookie('cookie_name');
Request::url();
Request::fullUrl();
Request::method();
Request::isMethod('POST');
Request::file('photo');
Request::file('photo')->isValid();
Request::file('photo')->upload($destination);
Request::hasFile('photo');
// Form POST: validate then escape at render.
$data = Request::all();
$validator = new \App\Core\Validator();
$validator->check($data, [
'email' => \App\Core\Rules::set()->isRequired()->isEmail(),
]);
if ($validator->fails()) {
return $this->view('form', ['errors' => $validator->errors()]);
}
// JSON body: {"email":"[email protected] "} reads the same way.
$email = Request::input('email');
// Files: check hasFile, validate, then upload (destination must be writable).
if (Request::hasFile('photo')) {
$file = Request::file('photo');
if ($file->isValid()) {
$file->upload(APP_ROOT . '/public/uploads');
}
}
$router->post('/form', [WelcomeController::class, 'formSubmit'])->middleware(CsrfMiddleware::class);
$router->group(['middleware' => [new AuthMiddleware()]], static function ($r): void {
$r->get('/dashboard', static fn (): string => 'Dashboard');
});
if ($loginOk) {
SessionRateLimiter::clear('login:' . getIpAddress());
}
trans('errors.quired') ?? 'This field is
use Roolith\Caching\Cache\CacheFactory;
use Roolith\Event\Event;
if (!CacheFactory::has('users_by_id_1')) {
CacheFactory::put('users_by_id_1', User::orm()->where('id', 1)->get(), 3600);
}
$user = CacheFactory::get('users_by_id_1');
Event::listen('user.created', static fn (array $user): string => 'welcome:' . $user['email']);
Event::trigger('user.created', [['email' => '[email protected] ']]);
$validator = new Validator();
// String field: g enough'],
['name' => Rules::set()->isRequired()->minLength(10)->maxLength(20)]
);
// Array field: ()) {
// do something!
} else {
// ['name' => ['minLength'], ...]
$errors = $validator->errors();
}
$validator = new Validator();
$validator->check(
[
'name' => 'john doe long enough',
'email' => '[email protected] ',
'company' => '',
'age' => 18,
'url' => 'https://example.com',
],
[
'name' => Rules::set()->isRequired()->minLength(10)->maxLength(20),
'email' => Rules::set()->isEmail()->isRequired(),
'company' => Rules::set()->isRequiredIf('age:greater_than:10'),
'url' => Rules::set()->isUrl(),
'age' => Rules::set()->isNumeric(),
]
);
if ($validator->success()) {
// do something!
}
Sanitize::param('hello-world_1'); // slug-safe
Sanitize::email('[email protected] '); // email-safe
escape($raw); // render-time escaping for views
_::only(['name' => 'hadi', 'age' => 33], 'name');
_::only(['name' => 'hadi', 'age' => 33, 'something' => 'else'], ['name', 'something']);
_::drop([1, 2, 3, 4, 5]);
trans('errors.quired') ?? 'This field is
Settings::setLang('es');
Settings::getLang();
trans('errors.
Storage::setCookie('name', 'value', Carbon::now()->addMonths());
Request::cookie('name');
Storage::deleteCookie('name');
Storage::setSession('name', 'value');
Storage::deleteSession('name');
php roolith generate controller DemoController
php roolith generate model Product
php roolith generate middleware AuthMiddleware
php roolith migrate:create create_users_table
php roolith migrate
php roolith migrate:status
php roolith migrate:rollback create_users_table