PHP code example of rawebone / micro

1. Go to this page and download the library: Download rawebone/micro 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/ */

    

rawebone / micro example snippets




= new \Micro\Application();
$app->attach(\My\Controllers\Controller());
$app->run(); // Runs the application
$app->send(); // Sends the result back to the browser



namespace My\Controllers;

use Micro\Request;
use Micro\Responder;
use Micro\DefaultController;

class Controller extends DefaultController
{
    protected function configure()
    {
        $this->setUri("/hello/{name}")
             ->addMethod("GET")
             ->addCondition("name", "\w+");
    }

    public function accepts(Request $req)
    {
        // You can optionally override this method if you want to check any
        // details about the request; this can help filter out an invalid
        // request outside of handling it directly, like:
        
        return !$req->isAjax();
    }

    public function handle(Request $req, Responder $resp)
    {
        return $resp->standard("Hello, {$req->get("name")}");
    }
}



namespace My\Tests\Functional;

// You may want to create a custom case for your project.
// If using PHP>=5.4 you can use the trait Micro\Testing\ComposableTestCase.
use Micro\Testing\AbstractTestCase;

class MyControllerTest extends AbstractTestCase
{
    protected static $app;

    protected function getApplication()
    {
        if (!self::$app) {
            self::$app = 

namespace My\Controllers;

use Micro\Request;
use Micro\Responder;
use Micro\DefaultController;
use Micro\TraceableInterface;
use Psr\Log\LoggerInterface;

class Controller extends DefaultController implements TraceableInterface
{
    protected $tracer;

    // ...
    
    public function tracer(LoggerInterface $log)
    {
        $this->tracer = $log;
    }

    public function handle(Request $req, Responder $resp)
    {
        $this->tracer->critical("Help!");
    }
}


namespace My\Controllers;

// ...

class Controller extends DefaultController
{
    public function handle(Request $req, Responder $resp)
    {
        if ($this->myApplication()->doWork($settingA, $settingB)) {
            // Handle success
        } else {
            // Handle error
        }
    }
}


namespace My\Controllers;

// ...

class Controller extends DefaultController
{
    public function handle(Request $req, Responder $resp)
    {
        if ($this->doWork($settingA, $settingB)) {
            // Handle success
        } else {
            // Handle error
        }
    }

    protected function doWork($a, $b)
    {
        // Complicated API process
    }
}