Beta version! This documentation is still in beta and is not complete.

Pickle documentation

Official documentation of the Pickle framework / For version 1.0


Introduction

  • Item Name : Pickle framework
  • Item Version : v 1.0
  • Author : Antoine Bergerault

Pickle is a php framework made for modern web development. It is equiped with a solid user system through the entire script. It has an MVC architecture and a lot of methods made for a simple start for all of your projects.

Requirements #back to top

The technologies you have to get or to have in order to use this framework :

  • - A web server (xampp, wamp, laragon)
  • - PHP 7
  • - MySQL
  • - Composer

How to Install Pickle #back to top


Installation using git

You can use the command line if git is installed on your computer. Just run this code in the command interpreter : git clone https://github.com/Antoine-Bergerault/Pickle-framework.git

Configuration #back to top

Basic configuration

The file config.php located in the core/Tools directory is the configuration for the database (MySQL). You need to run the file(s) located in the sql folder in order to make the system work.

<?php

namespace Pickle\Tools;
class Config{
    static $host = "localhost";//host name
    static $username = "root";//username
    static $password = "";//password
    static $database = "pickle";//name of the database
    static $CacheDirectory = "/temp";//the directory where the cache will be stored
    static $env = 'DEV';//DEV or PROD
    static $devmail = null;
}

?>

MySQL database configuration

You need to run the file(s) located in the src/sql folder in order to make the system work.

Route management #back to top

Specify the routes

The routes have to be specified in the file core/Router/web.php file of your application.

How does it work ?

Basically, this file implements new routes, created by the Router object. The declaration of a route looks like that :

Router::method(path, function);

For example :

                        Router::get('/home', function(){
                            return 'Welcome !';
                        });

                        Router::post('/post', function(){
                            return view('posts/post');
                        });
                    

You can also use a controller to manage the request

Router::get('/home', 'MainController@home');
Going further with the routes :

As said in the introduction, Pickle comes with a rich user management system. You can control access to pages within this file.

For example, you can restric a route to only connected users :

Router::get('/profile', ['view' => 'user/profile'])->only_if(App::is_connected());

This method takes a boolean as first argument, and an extra callback function as second argument, if the first is not equal to true. The complete list of user management methods provided by the App object :

  • App::is_connected()
  • App::getid()
  • App::getname()
  • App::logout()
  • App::is_role(rolename)
  • App::is_available(permission_name)

The views #back to top

All the views have to be created in the Views directory. It is a simple php file containing html elements. You can use the integrated functions to interact with other files, use the App class in order to personalize the page depending on the user data and you can use all the php functions, loops...

To display a view, you can use the view() function :

                            view('name of the view');
                        
How to integrate templates to your views ?

You can do that by using the fillTemplate integrated function

                            fillTemplate(name, params)
                        

The templates #back to top

The templates are located in Templates.

This is the same principle as the views. You can use variables injected during the filling. An example :

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <?php loadCSS('default') ?>
    <?php if(isset($styles)) loadTemplateStyles($styles); ?>
    <title>
    <?= $title ?? 'Title' ?>
    </title>
</head>

<body>

    <?php view('components/header') ?>

    <?=$content?>

    <?php view('components/footer') ?>

    <?php if (isset($scripts)) loadTemplateScripts($scripts); ?>

</body>

</html>
                        

Integrated functions #back to top

Some functions are integrated in all of the framework. They are create in the core/Tools/function.php files. You can add or delete functions as you want

There is the list of included functions :

  • debug($arr)

    This function is just a var_dump function with html pre tags around it

  • redirect($path)

    This function makes a javascript redirection with to the path argument

  • view($name, $data = null)

    Render the view with the first argument name. If it is in a folder, use the / in the filename. The second argument is for asserting data using the compact php function

  • url($path = false)

    Return the absolute url of a given path. If not given, it uses the default path of the router

  • root()

    It returns the website root

  • route($name)

    It returns the absolute path of the route with the name provided

  • alert($str)

    Print a javascript alert box

  • get($url)

    It returns a jquery code for executing an ajax get request to the given url

  • loadCSS($name)

    It loads the css file whose the name is provided

  • loadJS($name)

    It loads the css file whose the name is provided

  • speaking_format($date)

    It returns the natural format of a delay between the current time and the date given. ex : 5 minutes ago

  • str_random($length)

    It returns a token whose the length is provided

  • listen()

    It runs an ob_get_clean and an ob_start if the current environnement is not set to 'DEV'

  • fillTemplate($name, $arr = [])

    It loads the template with the provided name, the current variables and some extra variables if the second parameter is provided

  • loadTemplate($name, $data = null)

    It loads the template with the provided name and add some extra variables if a second parameter is passed

  • loadTemplateStyles($styles)

    It executes the function loadCSS but you can pass either an array or a name

  • loadTemplateScripts($scripts)

    It executes the function loadJS but you can pass either an array or a name

  • standard_format($date)

    It returns a date format string with the 'jS F Y' format with de date given

The controllers #back to top

The controllers are located in src/Controllers. The controllers are made to perform php scripts and to use models in order to render the correct view with the correct data. A Controller looks like that :

                            <?php
                            
                            namespace Pickle;
                            class ExampleController {
                                
                                //You can add methods here

                                public function test(){

                                    $UserModel = new UserModel();
                                    $user = $UserModel->first()->where(['email' => '= ?'])->args(['test@example.com'])->run();
                                    $user = $user[0];
                                    return view('test', compact('user'));

                                }
                                
                            }
                            
                            ?>
                        

It must be save as ExampleController.php

The models #back to top

The models are used to perform database queries. The models are located in src/Models.

How to use a model ?

You need to load the model first :

                            $UserModel = new UserModel();
                        

You don't need any require.

Then you can perform requests. This are different methods for performing requests :

  • select($arg)

    Select the list of column. ex : $UserModel->select(['id', 'name']);

  • first()

    Get the first element

  • find($n, $f = 0)

    Get n results. Begining from the second parameter (0 by default)

  • where($arr)

    Adding a where statement which corresponds to the conditions given in the argument. For a safe query, you will need to use the args() method with it. ex : $UserModel->where(['name' = 'LIKE "?%"']);

  • args($arr)

    Pass a list of values for a safe query. Works with the where method. ex : $UserModel->where(['name' => 'LIKE "?%"'])->args(['Ant']);

  • orderByDesc($param)

    Order the results by desc by the given param.

  • orderByAsc($param)

    Order the results by asc by the given param.

  • create($arg, $table = false, $pass = true)

    Create a new element into the table (by default the table of the model, but it can be changed using the second parameter). If the third argument is set to true (it is by default), the 'pass' corresponding value is automatically hashed (using PASSWORD_BCRYPT). ex : $UserModel->create(['firstname' => 'John', 'lastname' => 'Doe', 'pass' => 'eb7v2SU83']);

  • reset()

    Reset all the settings (except the table name).

  • sql()

    Get the sql query.

  • query($sql, $data = array())

    Performs a query.

Warning !

Don't forget to use the run() method at the end in order to get the results (you don't need that for the create method).

An exemple :
                            $UserModel = new $UserModel();
                            $UserModel->find(10)->select(['name', 'email'])->where(['name' => 'LIKE ?%'])->args(['Jo']);
                            $users = $UserModel->run();
                        

How to create a new model ?

First you have to choose the tablename of your new model.

Then create a file in src/Models like that :

TableModel.php

While "Table" is the name of the table in the database (singular not plurial).

Then fill in the file like this :

     
                            <?php
                            
                            namespace Pickle;

                            //import the model reference
                            require_once 'default/Model.php';

                            class TableModel extends Model{//replace "Table" by the name of your table (singular)

                                //defined the table name for this model
                                public $table = 'table';//replace 'table' by the name of your table (plurial)
                            
                                /*
                                * Create some methods here if you need it
                                */

                            }

                            ?>
                        

And your model is operational

The middlewares #back to top

The middlewares are located in src/Middlewares. There are used to inject script which are common to several pages. For example, the CsrfMiddleware is used for pages which contains form(s). A middleware looks like that :

                            <?php
                            
                            class ExampleMiddleware {

                                /*
                                * The methods you want
                                */

                            }
                        

It has to be saved as ExampleMiddleware.php

The methods can be whatever you want.

How to use a middleware ?

They are two different methods to use the middlewares :

If you want to import it on each requests and you want to use the constructor :

Just go to public/index.php and edit/create this line :

App::activeMiddlewares(['example']);

If you want to use multiple middlewares you can fill the array.

If you want to use it inside a function or controller :

Do this like this :

App::middleware('example');

Middlewares by default

There is some middlewares integrated by default in the framework

  • CsrfMiddleware

    A middleware made to integrate csrf inputs into your forms and to verify your forms when they are submitted. It is a tool made to strengthen the security of your forms.

  • MustBeConnected

    All is in the name. A middleware that deletes all the routes if you are not connected instead of the login route(s), and redirect the user to a specific route to get connected.

  • PdfMiddleware

    A really simple middleware that can return a pdf to the browser by settings all the headers needed. The path of the file from the data directory is given as a parameter (without the extension .pdf).

  • SecureContentMiddleware

    A middleware that can delete the script tags from an html code, and that can convert a string to an html code.

Realtime loading #back to top

The Pickle framework offers you a way to reload your pages using javascript in order to relaunch the php script.

On pages with this system activated, you can generate your pages using javascript. It offers you the opportunity to defer your entire php script, and to reload it multiple time in order to maintain data up to date. It is accessible from html element with the "data" class.

data-key

Print the content of the variable whose the name is given.

                            

data-property

Print the property of the object whose the name is given.

                            

data-foreach

Iterate over an array. Works on different depth levels.

                                

Name:

Name

Email:

Email

data-if

Use a condition with a boolean variable.

                            

data-explore

Explore an object. Used to print properties without redefinding data-key.

                            
name..

data-transform

Execute a function with the data given and print the result of this function.

                            

data-onload

Execute a function using the element as an argument.

                            
text

Realtime loading #back to top

The Pickle framework offers you a way to reload your pages using javascript in order to relaunch the php script.

On pages with this system activated, you can generate your pages using javascript. It offers you the opportunity to defer your entire php script, and to reload it multiple time in order to maintain data up to date. It is accessible from html element with the "data" class.

data-key

Print the content of the variable whose the name is given.

                            

data-property

Print the property of the object whose the name is given.

                            

data-foreach

Iterate over an array. Works on different depth levels.

                                

Name:

Name

Email:

Email

data-if

Use a condition with a boolean variable.

                            

data-explore

Explore an object. Used to print properties without redefinding data-key.

                            
name..

data-transform

Execute a function with the data given and print the result of this function.

                            

data-onload

Execute a function using the element as an argument.

                            
text