Skip to content

Apantos is a fast and simple framework based on php with security methods and dedicated orm. Modularly

Notifications You must be signed in to change notification settings

alishahidi/apantos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Apantos doc - main

TABLE OF CONTENTS

What is apantos

Apantos is a fast and simple framework based on php with security methods and dedicated orm. Modularly

How create first Apantos project

This project available on composer packagist you can easily install by composer create-project

composer create-project alishahidi/apantos

for serve project on port 8000

php -S 127.0.0.1:8000 -t public

Set tokens

after create project you must set .env CRYPT_TOKEN & TOKEN variable by default /api/token url set for get valid token using this url 2 time and save gived token into env variables

recommended remove token api route after saving token from /routes/api

What is framework architecture ?

this framework use mvc architecture

models in app/Models views in resources/view controllers in app/Controllers

Directory Structure

- app
  - Http
    - Controllers
    - Request
    - Services
  - Models
  - Providers
- bootstrap
  - /bootstrap.php/
- config
  - /app.php/
  - /database.php/
  - /image.php/
  - /mail.php/
- database
  - migrations
- public
  - /index.php/
- resources
  - view
- routes
  - /api.php/
  - /web.php/
- storage
  - fonts
  - images
- system

Directory explanation

app

Important directory contain controllers and request and .... for manage routes handlers and check form input and more

Http

Contain web request handlers and services

Controllers

Management classes for routes

standard name: NameController.php

Request

User input checkers

standard name: NameRequest.php

Services

Refactored classes

standard name: Name.php

Models

Database Models

standard name Name.php Use singular nouns

Providers

Providers run each request if stored in config file

standard name: NameProvider.php

bootstrap

contain bootstrap.php file

The job of this file is to load the framework

public

this direcotry serve as root directory

every request must be redirect to index.php file

resources

contain view direcotry

view

contain views direcotry & php file

standard name for use apts template engine: view.apts.php standard name for normal use without template engine: view.php

routes

web.php

for web request routes

api.php

for api request routes

storage

for in project files ex: files used for packages

system

kernel of framework

Routing system

all routes available in routes/{web, api}.php file

How create route

Note

web route start from / api routes start from /api

Argvs

  1. url
  2. Controller with namespace & class function name after @
  3. route name

Get

Route::get('/', "Home\HomeController@index", 'home.index');

Post

Route::post('/login', "Auth\LoginController@login", 'auth.login');

Put

Route::put('/admin/article/update/{id}', "Admin\ArticleController@update", 'admin.article.update');

Delete

Route::delete('/admin/article/delete/{id}', "Admin\ArticleController@destroy",'admin.article.delete');

Controllers

controllers called by routing system

controllers must be set in Route method

create your Controllers in app/Http/Controller like this

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        return "Hi";
    }
}

for use this example you must set Route for called index method in HomeController

Route::get('/', "Home\HomeController@index", 'home.index');

now if open / url in your browser you can see “Hi” message;

Model

create your models in app/Models like this

namespace App\Models;

use System\Database\ORM\Model;
use System\Database\Traits\HasSoftDelete;

class User extends Model
{
    use HasSoftDelete;

    protected $table = 'users';

    protected $fillable = ['name', 'email', 'password', 'avatar', 'permissions', 'bio'];

    protected $casts = ['permission' => 'arrray']
}

use Use singular nouns for Model name and set full name of table in protected $table

you must set fillable table column in protected $fillable id, create_at, updated_at, deleted_at exist by default in fillables

casts can convert arrays to safe string for stored in database and can convert string to array when you get record from database

Orm

Example tables

users

idusernamepasswordphone_number
1alitest+11843019
2alextest+32095u023
3poptest+3925253

categories

idname
1linux
2emacs
3php

tags

idname
1linux
2emacs
3php
4json

posts

idtitlecat_iddescription
1post number 11description of post number 1
2post 21description of post number 2
3post number 32description of post number 3
4post 43description of post number 4

post_tag

idpost_idtag_1
111
212
321
423

comments

iduser_idpost_idcomment
112comment 1
222comment 2
311comment 3

create

add record

argvs

  1. values:array

use

$user = User::create([
    'username' => 'ali',
    'password' => 'test',
    'phone_number' => '+319021243'
]);

$insertId = $user->insertId;

or

$user = new User();
$user->username = 'ali';
$user->password = 'test';
$user->phone_number = '+30231234401';
$user->save();

update

update record

argvs

  1. values:array => with primary id

use

$user = User::update([
    'id' => 1,
    'username' => 'alishahidi'
]);

// change ali username to alishahidi

or

$user = User::find(1);
$user->username = 'alishahidi';
$user->save();

delete

delete record

argvs

  1. primary id

use

User::delete(1);

all

give all records

use

$users = User::all();
foreach($users as $user)
    echo $user->useranem;

// output

    // ali
    // alex
    // pop

find

give user where id = $id

argvs

  1. primary id

use

$user = User::find(1);
$username = $user->username; // return ali

where

add where condition in query

argvs

if pass 2 argument it set operatino to =

  1. attribute
  2. value

if pass 3 argument it get operation from argument 2 and get value from argument 3

  1. attribute
  2. operatino
  3. value

use

// get first record
$post = Post::where('title', 'post number 1')->get()[0];
$title = $post->title; // return "post number 1"

or

// return all record contain "number" in title
$posts = Post::where('title', 'LIKE', "%number%")->get();
foreach($posts as $post)
    echo $post->title

// output

    // post number 1
    // post number 3

whereOr

like where but with OR operation

whereNull

argvs

  1. attribute

use

// get records if cat_id is null
$posts = Post::whereNull('cat_id')->get();

whereNotNull

argvs

  1. attribute

use

// get records if cat_id is not null | is set
$posts = Post::whereNotNull('cat_id')->get();

whereIn

argvs

  1. attribute
  2. values:array

use

// get posts recotds if cat_id in 1, 2, 3
$posts = Post::whereIn('cat_id', [1, 2, 3])->get();

whereBetween

argvs

  1. attribute
  2. from
  3. to

use

// get records if id between 1..3
$posts = Post::whereBetween('id', 1, 3)->get();

randomOrder

randomize records order

argvs

  1. expression

use

$posts = Post::randomOrder('DESC')->get();

orderBy

argvs

  1. attribute
  2. expression

use

$posts = Post:orderBy('created_at', 'DESC')->get();

limit

argvs

  1. from
  2. number

use

// get first 3 records
$posts = Post::limit(0, 3)->get();

count

use

// get cound of records
$postsCount = Post::count(); // return 4

pagination

argvs

  1. perpage

use

// auto convert page_id with $_GET['_pageid']
$posts = Post::pagination(3);

Relationships

hasOne

argvs

  1. model class name
  2. foreign key
  3. local key

use

$user = Post::hasOne(User::class, 'user_id', 'id');

hasMany

argvs

  1. model class name
  2. foreign key
  3. local key

use

$comments = Post::hasMany(Comment::class, 'post_id', 'id')->get();

belongsTo

argvs

  1. model class name
  2. foreign key
  3. local key

use

$user = Post::belongTo(User::class, 'user_id', 'id')->get();

belongsToMany

argvs

  1. model class name
  2. pivot table
  3. local key
  4. pivot foreign key
  5. pivot other foreign key
  6. foreign key

use

$tags = Post::belongsToMany(Tag::class, 'article_tag', 'id', 'post_id', 'tag_id', 'id')->get();
// |      *----------------------------------------------*        |         |       |
// |      *-------------------------------------------------------*         |       |
// *------------------------------------------------------------------------*       |
// *--------------------------------------------------------------------------------*

View

all views create in resources/view

apts template engine

- resources
  - view
    - home
      - layouts
        - master.apts.php
        - head-tag.apts.php
      - index.apts.php

home > layouts > master.apts.php

<!DOCTYPE html>
<html lang="en">

<head>
    @include('home.layouts.head-tag')
    @yield('title')
    @yield('head-tag')
</head>

<body>
    @yield('content')
</body>

</html>

home > layouts > head-tag.apts.php

<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

home > index.apts.php

@extends('app.layouts.app')

@section('head-tag')

<title>Apantos project</title>

@endsection

@section('content')

<h2>Welcome to apantos project</h2>

@endsection

render

replace / with . in your path path start in resources/view

view('home.index');

or

$message = 'Send message to view';
view('home.index', compact('message'));

example using in controller

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    public function index()
    {
        $message = 'Send message to view';
        return view('home.index', compact('message'));
    }
}

Auth system

auth using User mdoel by default

registerUser

argvs

  1. values:array
  2. password input name
  3. encrypt input name:array

use

$inputs = [
    'username' => 'alishahidi',
    'password' => 'decoded-secret-from-form',
    'phone_number' => '+13924324'
    'secret' => 'top secret'
];

Auth::storeUser($inputs, 'password', ['secret']);

updateUser

argvs

  1. values:array
  2. allowed inputs:key=>value array
  3. password input name
  4. encrypt input name:array

use

$inputs = [
    'id' => 1,
    'username' => 'ali',
    'password' => 'decoded-secret-from-form',
];

Auth::updateUser($inputs, ['id', 'username', 'password'], 'password');

loginUsingEmail

argvs

  1. email
  2. decoded password
  3. no user exist error message (opt)
  4. password wrong error message (opt)
  5. remember user (opt)
  6. user cookie validate time (opt)

use

Auth::loginEmailUsername('[email protected]', 'secret', "Username wrong.", "Password wrong", true, 4 * 24 * 60 * 60);

loginUsingUsername

like loginUsingEmail but send username between email in first argument

loginUsingId

argvs

  1. id

use

Auth::loginUsingId(1);

logout

use

Auth::logout();

check

check user login => redirect to auth.login route name if not login

use

Auth::check();

checkLogin

check user login => return true/false

use

$isLogin = Auth::checkLogin();

user

return user if login

use

$user = Auth::user();

userUsingEmail

use

$user = Auth::userUsingEmail('[email protected]');

userUsingUsername

use

$user = Auth::userUsingUsername('ali');