Giter Site home page Giter Site logo

codacy-badger / phreak Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webdevlabs/phreak

0.0 0.0 0.0 397 KB

Phreak! ultra-light fast php framework for lamez :)

License: MIT License

PHP 80.12% Smarty 6.26% CSS 8.64% JavaScript 3.73% HTML 1.25%

phreak's Introduction

Phreak!

Build Status Codacy Badge SensioLabsInsight StyleCI

ultra-light fast php framework powered by:

Installation

composer create-project webdevlabs/phreak

Serve app localy with the built-in PHP web server

php -S localhost:8000 -t public

Autoloader

Very simple and understandable using the Namespace as file path. All directory names are converted to lowercase, file cases are kept as written. Example:

<?php
use App\Controllers\Front;

will autoload the file \app\controllers\Front.php

Simple Routing

Calls the App\Controllers\User::displayUser($id) method with {id} parameter as an argument

$router->get('/users/{id}', ['App\Controllers\User','displayUser']);

Controller Routing

Calls the App\Controllers\Front.php with the proper request method.

$router->controller('/', 'App\Controllers\Front');    

Advanced Routing

Filters

Calls class App\Models\Auth with method checkLogin() and break on return false.

$router->filter('auth', ['App\Models\Auth','checkLogin']);

Group all requests under http://host/profile/ through filter

$router->group([
        'prefix'=>'profile', 
        'before'=>'auth'
    ], 
    function ($router) {
        $router->controller('/', 'App\Controllers\Account\Profile');    
    });

More advanced routings can be found at Phroute's page.

Validation

    public function postComment () 
    {
        $input  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
        $val = new \System\Validation($input);
        $val->addRule('name', 'Empty name field', ['required']);
        $val->addRule('comment', 'Empty comment field', ['required','minLength=5']);
        $val->addRule('comment', 'Comment too big', ['maxLength=500']);
        if ($val->validate()) {
            echo 'Comment post ok';
        } else {
            $this->template->assign('errors',$val->getErrors());
            $this->template->display("errors.tpl");
        }        
    }

Simple controller

<?php

namespace App\Controllers;

use System\Template;
use System\Language;

class Front
{
    private $template;
    private $language;

    public function __construct (Template $template, Language $language) 
    {
        $this->template = $template;
        $this->language = $language;
    }

   public function getIndex()
    {
        $this->template->assign('title', 'Phreak!');
        $this->template->assign('languages', $this->language->available_languages);  
        $this->template->display('layout.tpl');
    }    
}

More advanced examples can be found in the 'modules' directory.

Events

use System\Event;
class ... {
    public function __construct (Event $event) 
    {
        $this->event = $event;
        $this->event->bind('someEventName', function () { echo "stay foolish"; });
    }
    public function getSome () {
        $this->event->trigger('someEventName');
    }
}

Database

You now have different options for managing your database. There are 2 existing database drivers. Sample database wrapper class DB:: and the well known database mapper Eloquent.

You control what database driver to load directly from config/database.php

Example PDO driver fetch queries:

 * Result: single column
 * $count = DB::column('SELECT COUNT(*) FROM `user`);

 * Result: an array(key => value) results (i.e. for making a selectbox)
 * $pairs = DB::pairs('SELECT `id`, `username` FROM `user`);

 * Result: a single row result
 * $user = DB::row('SELECT * FROM `user` WHERE `id` = ?', array($user_id));

 * Result: a single row result
 * $user = DB::row('SELECT * FROM `user` WHERE `id` = :varname', array(":varname"=>"some variable"));

 * Result: an array of results
 * $banned_users = DB::fetch('SELECT * FROM `user` WHERE `banned` = ?', array(TRUE));
 
 * Result: any query
 * $dosql = DB::query('UPDATE `settigs` WHERE `setid`=?', ['somesetid']);
 
 * Result: insert user
 * DB::insert('users', ['username'=>'test','password'=>'testpass']);

 * Result: update user password where user_id = 1
 * DB::update('users', ['password'=>'testpass'], '1', 'user_id');

The new available method for managing your database is Eloquent (from the Laravel framework).

The Eloquent ORM provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Examples of phreak eloquent usage can be found at the 'modules/eloq' made for demonstration.

Full documentation can be found at Eloquent's page.

Console Commands

You can also execute phreak controllers from your console/crontab by calling the console.php file where the parameters are the filename of the controller.

Calls the App\Commands\SomeController.php with the proper request method.

php console.php SomeController

All console controllers are located in the "app/commands" folder. The folder location is defined in the 'app/ConsoleController.php' and can be changed.

phreak's People

Contributors

webdevlabs avatar gitter-badger avatar dependabot[bot] avatar

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.