https://github.com/aminrafiei/php-framework
A lightweight PHP micro-framework
https://github.com/aminrafiei/php-framework
framework php php-framework
Last synced: 2 months ago
JSON representation
A lightweight PHP micro-framework
- Host: GitHub
- URL: https://github.com/aminrafiei/php-framework
- Owner: aminrafiei
- License: mit
- Created: 2018-12-07T20:21:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-11-06T20:37:08.000Z (over 1 year ago)
- Last Synced: 2026-01-11T15:39:03.515Z (3 months ago)
- Topics: framework, php, php-framework
- Language: PHP
- Homepage:
- Size: 191 KB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
PHP micro-framework
---------------------
## Overview:
- #### This is a a lightweight PHP micro-framework that has syntax like Laravel framework
----------------------
## :bulb: Documentation & Sample Code:
### Sample:How to add a route?
You can add your route in routes folder in routes.php.
You can use GET, POST, DELETE methods for your routes.
```php
use Core\Router;
Router::METHOD({path}, {clouser} | {controller-name@method-name})
```
Sample route :
```php
use Core\Router;
Router::make()->get('hellowrold',function (){
return "Hello World!";
});
Router::make()->post('home','PagesController@home');
```
### Sample:how to use Dependency Injection?
When you call Controller that has dependency, framework automatically inject a instance of that Class/Interface .
if you inject interface you must specify which implementation of that interface should bind in bootstarp.php!
```php
/**
* Class bootstrap
*/
class bootstrap
{
/**
* @var array
*/
public static $binds = [];
/**
* @var array
*/
public static $registers = [
SomeInterface::class => SomeService::class,
];
...
}
```
Example:
```php
class AuthController extends BaseController
{
protected $user;
public function __construct(User $user, SomeInterface $someInterface)
{
$this->user = $user;
}
...
```
---------------------
### Sample:how to use Middlewares?
You can use middleware for all routes or specific Controller by register your middleware in ```$middlewares``` and for all routes ```$routeMiddlewares```
you can register in bootstrap.php file
```php
/**
* Class bootstrap
*/
class bootstrap
{
...
/**
* apply this middlewares for all routes
*
* @var array
*/
public static $routeMiddlewares = [
'trim',
];
/**
* register middlewares
*
* @var array
*/
public static $middlewares = [
'trim' => \Core\Kernel\Middleware\Rules\Trim::class,
'auth' => \Core\Kernel\Middleware\Rules\Auth::class,
];
}
```
Example:
```php
class PagesController extends BaseController
{
//without params
static $middleware = 'auth';
//also you can send params to your middleware
static $middleware = ['role' => 'admin'];
...
```
```php
class Middleware implements MiddlewareContract
{
/**
* you can handle validation by return boolean!
* "true" for accept and "false" for failed
*
* @param array|null $params
* @return bool
*/
public static function handle(array $params = null): bool
{
...
return true;
}
/**
* Error message if middleware validation failed
*
* @return string
*/
public static function message(): string
{
return ...;
}
}
```
---------------------
### Sample:how to use ORM?
The framework has simple orm that you can CRUD and use "Where" clause for add condition in your query.
```php
$this->user->where('name', 'Amin', 'LIKE')
->andWhere('age', 20, '=')
>get();
```
---------------------
### Sample:how to use validation?
The framework has simple validation that you can validate "Email, Number, Required"
and it returns true/false
```php
$request = request()->get();
$validation = validation()->validate($request, [
'name' => ['required'],
'username' => ['required', 'email'],
'password' => ['required'],
]);
```
---------------------
### Sample:how to use cache?
The framework has cache that implement Redis as default
you can change it in bootstrap.php file
```php
cache()->remember('someThing',function (){
return $this->user->first();
},999)
```
---------------------