Giter VIP home page Giter VIP logo

lego-framework's Introduction

LEGO FRAMEWORK

PHP Framework built from a combination of several libraries.

Installation

composer create-project akbaraditamasp/lego-framework:dev-main example-app

Then run the following command in the terminal to start the localhost web server.

php adel run

Routing

The route file is in public/index.php

Routing in Lego uses the bramus/router library. Below is a simple example.

    $app->route("GET", "/", function() {
        return ["message" => "Hello World!"];
    });

The route handler can also reference a controller method.

    $app->route("GET", "/", "Controller\\Welcome::index");

For more details, please visit the documentation from bramus/router.

Controller

Controllers are the de facto way of handling HTTP requests in Lego. They enable you to clean up the routes file by moving all the inline route handlers to their dedicated controller files.

In Lego, the controllers are stored inside (but not limited to) the controllers/ directory and each file represents a single controller. For example:

    <?php
    namespace Controller;
    
    use Lego\App;
    
    class Welcome
    {
    	public static function index(App $app)
    	{
    		return ["message" => "Hello World!"];
    	}
    }

Make Controller Command

You can make use of the following php adel command to create a new controller.

php adel make:controller Post

Request

Request in Lego using library sabre.io/http.

You can access the request object from the HTTP context passed to the route handler.

    $app->route("GET", "/", function(App $app) {
	    $body = $app->request->getPostData();
	});

Response

Response in Lego using library sabre.io/http.

You can access the response object from the HTTP context passed to the route handler.

    $app->route("GET", "/", function(App  $app) {
    	$app->response->setStatus(200);
    	$app->response->setHeader("Content-Type", "application/json");
    	$app->response->setBody(json_encode([
    		"message" => "Hello World!"
    	]));  
    
	    $app->finish();
    });

File Uploads

Lego provides you an API for dealing with file uploads.

You can access the files using the $app->request->getFiles() method.

    $app->route("POST", "/", function(App  $app) {
	    $image = $app->request->getFiles()["image"];
    
	    $image->move(__DIR__);
    });

Validation

Lego uses the rakit/validation library to do validation. For example:

    $app->route("POST", "/", function(App $app) {
	    $app->validate([
		    'name' => 'required',
		    'email' => 'required|email',
		    'password' => 'required|min:6',
		    'confirm_password' => 'required|same:password',
		    'avatar' => 'required|uploaded_file:0,500K,png,jpeg',
		    'skills' => 'array',
		    'skills.*.id' => 'required|numeric',
		    'skills.*.percentage' => 'required|numeric',
	    ]);
    });

Database

Lego uses Eloquent as an ORM.

You can set the database configuration in the .env file

Creating Your First Model

php adel make:model Post

You can also generate the migration alongside the model by defining the -m flag.

php adel make:model Post -m

Migrations

Lego uses phinx as schema migrations.

You can create a new migration by running the following Adel command. The migration files are stored inside the db/migrations directory.

php adel migration create Post

Run and Rollback

Once you have created the migration files you need, you can run the following Adel command to process migrations. For example:

php adel migration migrate

You can use Rollback command to undo previous migrations executed by Phinx.

php adel migration rollback

Auth

By default Lego uses the User and UserLogin models for the authentication process.

You can generate an API token for a user using the Auth::make method.

    $app->route("POST", "/login", function(App $app) {
	    $user = User::find(1);
	    
	    return  Auth::make($user);
    });

You can use $app->auth() method to guard the routes against the un-authenticated requests.

    $app->route("GET", "/", function(App $app) {
	    $app->auth();
    
	    return ["message" => "Hello World!"];
    });

Or you want to make authentication optional

    $app->route("GET", "/", function(App $app) {
        $app->auth(false);
    
        return ["message" => "Hello World!"];
    });

You can access user data via $app->user

    $app->route("GET", "/", function(App $app) {
        $username = $app->user->username;
    
        return ["message" => "Hello World!"];
    });

lego-framework's People

Contributors

akbaraditamasp avatar

Watchers

 avatar

Forkers

ekohendratno

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.