Skip to content

aminrafiei/php-framework

Repository files navigation

PHP micro-framework


Overview:

  • This is a a lightweight PHP micro-framework that has syntax like Laravel framework


💡 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.

use Core\Router;

Router::METHOD({path}, {clouser} | {controller-name@method-name}) 

Sample route :

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!

/**
 * Class bootstrap
 */
class bootstrap
{
    /**
     * @var array
     */
    public static $binds = [];

    /**
     * @var array
     */
    public static $registers = [
        SomeInterface::class => SomeService::class,
    ];
    
    ...
}

Example:

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

/**
 * 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:

class PagesController extends BaseController
{
    //without params
    static $middleware = 'auth';

    //also you can send params to your middleware
    static $middleware = ['role' => 'admin'];

    ...
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.

$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

$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

  
  cache()->remember('someThing',function (){
              return $this->user->first();
  },999)