Koldy

Koldy is PHP framework built for developers who wants to build web easy, fast, scalable and secure.

I want this!

About

Koldy is young PHP framework with main target to become powerful, easy to use and well documented PHP framework. Everyone can use it - beginners, professionals, PHP haters and even Java developers who hates PHP :)! This framework is trying to be strict, so you'll have to write good code, or don't code.

Examples

<?php

class IndexController {

    public function 
indexAction() {
        return 
"Hello World";
    }

}
<?php

class UserController {

    public function 
loginAjax() {
        return 
Json::create(array(
            
'success' => true,
            
'login' => 'ok'
        
));
    }

}

Lets say you want to display user's login form and perform login action? All magic would happen in /application/controllers/UserController.php

View raw file

<?php

class UserController {

    
/**
     * Display login form
     * Handles URI /user
     * Handles URI /user/index
     */
    
public function indexAction() {
        return 
View::create('base')
            ->
with('page''login');
    }

    
/**
     * Handle POST request made with jQuery Ajax
     * Handles URI /user
     * Handles URI /user/index
     */
    
public function indexAjax() {
        
$validator Validator::create(array(
            
'email' => 'required|email',
            
'password' => 'required|min:5|max:32'
        
));

        
$uas Request::userAgent();

        if (
$validator->failed()) {
            
Log::notice("Bad login request using {$uas}");
            return 
Json::create(array(
                
'success' => false,
                
'message' => 'Your input is invalid',
                
'errors' => $validator->getMessages()
            ));
        }

        
$params $validator->getParamsObj();

        
$user User::fetchOne(array(
            
'email' => $params->email,
            
'password' => $params->password
        
));

        if (
$user === false) {
            
Log::notice("Failed login request using {$uas}");
            return 
Json::create(array(
                
'success' => false,
                
'message' => 'Wrong e-mail or password'
            
));
        } else {
            
$user->last_login_ip Request::ip();
            
$user->last_login_time Db::expr('NOW()');
            
$user->save();

            
Log::info("User #{$user->id} logged in using {$uas}");
            
Session::set('user'$user->getData());

            return 
Json::create(array(
                
'success' => true
            
));
        }
    }

}

This example assumes that you have base layout on /application/views/base.phtml and you're passing variable page with value login and we're assuming that page "login" is another .phtml file. So, in base.phtml, you'll have to render that page with <?= $this->render($this->page) ?>.

This example assumes that login is done with jQuery ajax in default route class environment. Thats why form will be POST type and will lead to the same url: /user.

Later on, first we do is input validation. We don't want to make any calls to database if user entered non-email text in e-mail input field. Database call will be done only if all fields are in correct format. Then, we take all parameters from validator class and then we try to load User object filled with data from database. We're assuming that you have model defined with name User. If login is successful, we'll write user's last login time and IP to database.

Returning JSON will output correct JSON string to browser with correct headers so jQuery will know how to handle the response.

Koldy.net © 2017