abdursoft php mvc framework. Just work with fun. It's supported multiple databases and a new template engine like blade. Now you can easily build your project with this framework. It has default jwt authentication system

1.0.8 2024-08-08 14:19 UTC

This package is auto-updated.

Last update: 2024-11-08 14:46:46 UTC


README

It's a custom php mvc framework developed by abdursoft.com. It's very easy to use and customizable. Now it's supported many features as like as Laravel and blade template.

We also build a template engin called ABS Template engine that integrated in this framework. It will make your code very simplified and powerful. Now you can use many thing that use on Laravel such as conditions, print, echo, for loop, foreach loop, switch case and so on.

You can also pass the page title, meta tags, custom styles, javascript and so on. More over you can easily use master layout and child component and extend them very easily. This template engin works as OOP architecture.

Before, you run the project, You need to enable some php extension from php.ini file.

sodium, tidy, zip, xsl,pdo,mysql,pgsql

Now let's start with Abdursoft Framework.

composer create-project abdursoft/php project_name

After installing the package update the composer.json file content with example.composer.json then update the composer

composer update

If the framework successfully installed on your device then go to the project/app directory. Then open the Karnel file from app\Karnel.php

public static function csrf() {
    return [
        'csrf'    => false,
        'encrypt' => true,
    ];
}

public static function layout() {
    return [
        'minify' => true,
        'mime'   => ['php', 'html', 'htm', 'abs'],
    ];
}

Now change the CSRF mode true/false. If you make this true then csrf service will start on the project otherwise it will be disabled

After that you have to change mime types as you want but remember that for other file's extension consistency. If you want a minify text when you go to page-source-view then make minify true otherwise false.

Now you have to set up some required variable on core\Config\Config.php

Now make the changes with your preferred data for these variables

define( "BASE_URL", 'http://domain.example/' ); //set root directory/domain
define( "SITE_TITLE", 'ABS MVC FRAMEWORK' ); //site name or title
define( "FAV_ICON", BASE_URL . "assets/images/premium.png" ); //site logo
define( 'DEFAULT_KEYWORDS', 'abs mvc developed by abdursoft' ); //Default keywords

define( 'DATABASE_SERVER', 'mysql' ); //supported database mysql,pgsql,mongodb

Moreover there is a lot of variable for some special functions and package just update that when you are going to use that package|function.

Lets make some route on the web/api file in the route/web.php or route/api.php

Route::get( '', [App::class, 'index'] );

Route::get( '/country', function ( ) {
    $request = new Request();
    echo $request->input('name');
}, ['name'] );

Route::get( '/layout', [App::class, 'layout'] );

Route::group( 'user', function () {
    Route::get( '/profile', [User::class, 'profile'] );
} );

Route::prefix( 'auth' )->group( 'zeroUser', function () {
    Route::post( '/login', [User::class, 'login'] );
} );

Route::withMiddleware( [Authentication::class,'checkAuth'], function () {
    Route::get( '/auth', [App::class, 'auth'] );
} );

Now need to create/update the controller class in app\Controller\

App.php

namespace ABS\Framework\App\Controller;

use ABS\Framework\Core\Files\Files;
use ABS\Framework\System\Processor\Controller;
use ABS\Framework\System\Request\Request;

use function ABS\Framework\System\Helper\view;

class App extends Controller {
    public function __construct() {
        parent::__construct();
    }
    public function index() {
        $this->load->page_title = "Input validation";
        $this->load->view( 'form');
    }

    public function view() {
        return->view( 'form');
    }

    public function layout() {
        $this->metaContent( 'Hello layout', 'Kmn acho tumi' );
        $this->loadStyle('/css/style.css');
        $this->load->view( 'form' );
    }

    public function auth() {
        echo "Welcome to the auth page";
    }
}

Now need to create/update the controller class in app\Controller\

User.php

namespace ABS\Framework\App\Controller;

use ABS\Framework\System\Auth\Auth;
use ABS\Framework\System\Auth\Session;
use ABS\Framework\System\Processor\Controller;
use Exception;

use function ABS\Framework\System\Helper\response;

class User extends Controller {
    public function __construct() {
        parent::__construct();
    }
    public function profile( ) {
        try {
            $user = Auth::jwtDecode( $this->request->input['token'] );
            $this->response( [
                'message' => 'server is ok',
                'data'    => $user,
            ], 200 );
        } catch ( Exception $e ) {
            $this->response( [
                'status'  => 0,
                'message' => $e->getMessage(),
            ], 200 );
        }
    }

    public function login( $param ) {
        if ( !empty( $param ) ) {
            $token = Auth::jwtAUTH( $param, 'users' );
            Session::set( 'jwt_token', $token );
            return response( [
                'message'    => 'Login successful',
                'token'      => $token,
                'token_type' => 'Bearer',
            ], 200 );
        }
    }
}

Lets create/update Authentication.php middleware file in app\Middleware directory.

namespace ABS\Framework\App\Middleware;

class Authentication {

    public function checkAuth($callback){
        if('hello' == 'hello'){  // your staff
            call_user_func($callback);
        }
        return;
    }

    // Route::get( '/auth', [App::class, 'auth'] ); this route will protect with the checkAuth middleware function. So you can add your condition and return the callback.
}

Now lets run the project for first time.

php -S localhost:9000

Routing system for the web and api

web routes
https://domain.example/path_uri
api routes
https://domain.example/api/path_uri

If the project is running successfully then you can test the all routes. Now time to update the layout page and children component. -First create a components folder/directory in public/view/ then create some files such as header.php and footer.php and layout.php

header.php
<h2>header component</h2>
footer.php
<h2>Footer Component</h2>
layout.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@title@</title>
    @style@
    @script@
    @meta@
</head>
<body>
    @addView('components/header')
    @import(content)
    <img src="{{assets('171924459195spa02ei31jbss784seum66io892spi.png')}}" alt="" width="400px" height="600px">
    @import(body)
    @addView('components/footer')
</body>
</html>

Now lets create a child view/component in public/view

form.php

@extends('components/layout')
@title(layout page)
@export(body)
<h3>Hello text4</h3>
<div>
    <h4>Lorem Text</h4>
    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dignissimos explicabo, quod quibusdam qui accusantium laborum incidunt unde officiis veniam sunt placeat? Nulla illo, molestiae nam ratione numquam quod pariatur quo optio nobis porro unde eius natus quis earum quam in.</p>
</div>

<form action="/action" method="post" enctype="multipart/form-data">
    @csrf
    <input type="text" name="name" value="">
    <input type="text" name="phone">
    <input type="file" name="file" id="">
    <input type="submit" value="submit">
</form>

@endExport

@export(content)
<h3>Content Body</h3>
@endExport

@title@ will inherit the page title from controller @meta@ will inherit the meta content from controller @style@ will inherit the styles single|array from controller @script@ will inherit the scripts from controller @addView(page) will include page.php from public/view directory @import(content) will receive @export(content) from child component @extend(layout) will inherit the main layout component and data @export(content) will export @export(content) data from child to layout component @Session($item) Will print the session value of the $_SESSION[$item] @Text($nice) Will print the multi lang value of $nice @echo($nice) Will print the $nice variable @printf($nice) Will print the $nice variable @print_r($nice) Will print the $nice variable {{$title}} will print the $title value {{assets('file_path/file_name)}} will include the assets file from assets directory {{storage('file_path/file_name)}} will include the storage file from storage/uploads directory {{resource('file_path/file_name)}} will include the resource file from public/resource directory {/ php_code /} execute the all PHP codes without <?php ?> starting and ending sign

Others template directives

@if('me' === 'me')
    <h2>Equal</h2>
    @elseif(3==3)
       <h2>Else Done</h2>
    @else
        <h2>Not Equal</h2>
@endif

@for($i=0; $i < 10; $i++)
    <h2>{{ $i }}</h2>
@endfor


@foreach($data as $key)
    <h2>{{$key}}</h2>
@endforeach


@switch('hello')
    @case(3)
        @echo('nice match')
        @break
    @case(hello)
        @echo('Hello World')
        @break
    @default
        @echo('nothing')
@endswitch

<h3>Database and Model</h3> To create a model file open the directory app/Model then make a new php file according your table name. If you have a table name users in your database you have to create the model as Users.php and the inner content should be

namespace ABS\Framework\App\Model;

use ABS\Framework\DB\Model;

class Users extends Model{
    protected static $table;
}

You can also change the table with protected static $table='name_of_the_table'

<h4>Whats the facility of a model</h4>

-Data insert -Data read -Data update -Data delete -Data aggregate -Data joining

<h4>How to insert data with a model</h4>

Route::post( '/user-create', function ( ) {
    $request = new Request();
    Users::create([
        'name' => $request->input('name'),
        'email' => $request->input('email'),
        'phone' => $request->input('phone'),
        'gender' => $request->input('gender')
    ]);
    echo 'User successfully created';
}, ['name'] );

[N.B] Users model use alias is add your control/route page.

<h4>How to update data with a model</h4>

Route::post( '/user-update', function ( ) {
    $request = new Request();
    Users::where('id','=',1)->update([
        'name' => $request->input('name'),
        'email' => $request->input('email'),
        'phone' => $request->input('phone'),
        'gender' => $request->input('gender')
    ]);
    echo 'User successfully updated';
}, ['name'] );

<h4>How to fetch all data from a model for a single user</h4>

Route::post( '/user-get-all-single', function ( ) {
    $request = new Request();
    Users::where($column,$operator,$value)->last();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>How to fetch specified data from a model for a single user</h4>

Route::post( '/user-get-specified', function ( ) {
    $request = new Request();
    Users::select(['name','phone'])->where($column,$operator,$value)->last();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>How to fetch all data from a model </h4>

Route::post( '/user-get-all', function ( ) {
    $request = new Request();
    Users::where($column,$operator, $value)->get();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>How to fetch all data from a model with orderby and limit </h4>

Route::post( '/user-get-oder-all', function ( ) {
    $request = new Request();
    Users::orderBy('id', 'DESC')->where($column,$operator,$value)->limit(3,10)->get();
    echo 'User successfully retrieved';
}, ['name'] );

<h4>How to fetch all data from a model with joining </h4>

Route::post( '/user-get-oder-all', function ( ) {
    $request = new Request();
    $all_items = Items::where('item_id','=',42)->leftJoin('item_category','item_category.id','=','items.category_id')->get();
    echo 'Items successfully retrieved';
}, ['name'] );