Dawn Micro-Framework Kullanımı

Doğukan Akkaya

Dawn Micro-Framework

To install : https://github.com/dogukanakkaya/Dawn-Framework

I ashame while im saying this 'a framework', but for me this is a little, flexible and improvable framework that you can use in your little projects.

i recommended you to look at the example and you get a better understanding.

First of all you need to run "composer install" command to install ORM and DB Usage, Template Engine and Console Component.

composer install

After that first thing you should do is to create a .htaccess file. .htaccess file content

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php [L]

After you create the .htaccess file. You can run php bin/console.php make:controller DawnController command from terminal.

Or you can create a php file in app/Controller directory. And for routing you can add the route like :

$router->get('/','App\Controller\DawnController@index');

For more documentation of route class you can visit https://github.com/bramus/router

If you created the controller with command then you already have a function called view() If you haven't then you can call your views like

$this->view("home/index",["controllerName" => "Dawn"]);

And don't forget to set your url in app/Config/config.php file You can use it like

echo url("public/css"); 
echo realUrl("path");

You can set many things in config.php file like your default language. You can also set your language like:

getLanguage("tr"); // If no parameter you give, then returns the language.
// You can change your language and validation messages, or other messages from this function dynamically.

Afterwards the libraries that uses language messages will use language variable that given in app/Config/config.php file.

 

Validation

To use validation library you must include IO library.

 
$validation = new Validation();
$run = $validation->validate([
    "userEmail" => array("label" => "E-Mail Address", "rules" => "required|email"),
    "userPassword" => array("label" => "Password", "rules" => "required|min[6]"),
    "callback_test" => array("label" => "", "rules" => "required|callback[mycallback]")
    /* 
    For callback, you need to write a function named mycallback() 
    Do your validations there, and if everything is right, then just return false,
    Else return an array with status = false, and a key for validation message
        return ["status" => false, "key" => "phoneVal"];
    Key is used to return a message in case of error you can define it in app/Language/en/validation.json like:
    "phoneVal": "The {field} is wrong";
    Field is the label name that given in the validation.
    */
]);
// If validation went wrong then you can get errors like $validation->errors("",""); you don't need to set parameters.
if (!$run){
    // If validation returned false, write errors and stop the program with 'exit'
    ajaxResponse(0, "Error", $validation->errors());
    exit;
}

 

IO

IO::isAjax() # Check if coming request is an ajax request or not. 
IO::POST('post_name') # Returns the post value which given as parameter, if no parameter given then returns all the post.
IO::GET('get_name') # Same as the POST() but returns the GET values. 
IO::GET_POST('request_name') # Same as the post and get, returns the request values. 
// POST,GET,GET_POST functions returns the data filtered. trim and htmlspecialchars. So it's xss secured.

 

Session

$this->session->set("key","codethereal") // Value can be array to, writes a value to $_SESSION 
$this->session->get("key") // Returns the key which we wanted, if not exists then returns false. 
$this->session->getAll() // Returns the all $_SESSION variable. 
$this->session->remove("key") // Removes the session variable which wanted if exists. If not exists then returns false. 
$this->session->destroy() // Destroys the session.

 

Cache

You should set your cache directory inside app/Config/cache.php

// cache.php config file content : return ['dir'=>'app/Cache/']; 
$cache = new Cache(); 
if(!$array = $cache->get("words")) // If cache not exists, then save a new cache named "words" 
{ 
     $array = array("Doğukan", "Codethereal"); 
     $cache->save("words",$array);
}

You can also delete your cache, for more documentation visit https://github.com/onuryanmis/Php-Cache

 

Config

$this->config->load("filename") // You can load a config file, it must be in "app/Config" dir. 
$this->config->get("key") // Returns the key which we wanted, if not exists then returns false. 
$this->config->getAll() // Returns the all config variables. 
$this->config->set("key","value") // This function does not create a new config variable, it only changes the old value of the given key 
$this->config->setAll(["config_var"=>1]) // You can add new config variables with this function.

 

Loading Helper

helper("filename"); // It must be in "app/Helper" dir.
echo APPPATH; // "app/" dir.
echo SYSPATH; // "sys/" dir.

 

Database operations. For database operations we use Laravel's eloquent. To connect database, you can set your db options in "app/Config/database.php" file. After you connect to database, you can make database operations. In "app/Models" dir. You can look at the example. app/Models/User.php For more documentation visit https://packagist.org/packages/illuminate/database

 

Template Engine

# Public path to use css,js... files. 

{% set attr = {"id": "submit"}%} 
{{ form_open(attr) }} # Form open and Form close functions opens a form with given attrs and close it with a csrf token named __token__ 
# you can access this with $this->session->get("__token__"); 
{{ form_close() }}

You can also create your own twig functions in app/Twig dir. For more documentation of template engine visit https://twig.symfony.com/doc/3.x/

0 Yorum

İlk yorumu sen ekle!