PHP code example of r567tw / focus

1. Go to this page and download the library: Download r567tw/focus library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

r567tw / focus example snippets



app\controllers\MainController;
use r567tw\focus\core\Application;

// init our framework 
$app = new Application(dirname(__DIR__));

// you can create MainController.php or use **composer autoload**
$app->router->get('/',[MainController::class,'home']);

// or return string
$app->router->get('/hello', 'helloworld');

// or return callback
$app->router->get('/json',function(){
    return json_encode(['xxxx'=>'yyy']);
});

// run app
$app->run();

$app->on(Application::EVENT_BEFORE_REQUEST,function(){
    $time = date("F j, Y, g:i a");
    file_put_contents('.././logs/hello.log', "{$time} before request\n", FILE_APPEND);
});

$app->on(Application::EVENT_AFTER_REQUEST,function(){
    $time = date("F j, Y, g:i a");
    file_put_contents('.././logs/hello.log', "{$time} after request\n", FILE_APPEND);
});


namespace app\controllers;

use r567tw\focus\core\Application;
use r567tw\focus\core\Controller;
use r567tw\focus\core\Request;
use r567tw\focus\core\Response;
use app\middlewares\AuthMiddleware;

class MainController extends Controller
{
    public function __construct()
    {
        // you can create middleware
        $this->registMiddleWare(new AuthMiddleware(['contact']));
    }

    public function contact()
    {
        // response give you "file" for JSON response
        return response()->file(dirname(__DIR__)."/jsons/sample.json");
    }

    public function home(Request $req)
    {
        return response()->json(["hello"=> "world"]);
    }

    public function redirect()
    {
        return response()->redirect('https://tw.yahoo.com');
    }
}