PHP code example of jameslevi / dispatcher

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

    

jameslevi / dispatcher example snippets






// Make a new router instance.
$router = new Graphite\Component\Dispatcher\Dispatcher();

// Execute callback each time error occurs.
$router->onError(function($request) {

});

// Register middleware #1.
$router->middleware(function($request) {

});

// Register middleware #2.
$router->middleware(function($request) {

});

// Register route #1.
$router->get("/", function($request) {

    return "Home";
});

// Register route #2.
$router->post("/register", function($request) {

    return "Register";
});

// Register after middleware #1.
$router->afterMiddleware(function($request) {

});

// Execute callback each time request ends.
$router->onDestroy(function($request) {

});

// Run your application.
$router->run();

$router->get("/members", function($request) {});

$router->post("/members", function($request) {});

$router->put("/members/{id}", function($request) {});

$router->patch("/members/{id}", function($request) {});

$router->delete("/members/{id}", function($request) {});

$router->any("/dashboard", function($request) {});

$router->match(array("put", "patch"), "/member/{id}", function($request) {});

$router->onCreate(function($request) {});

$router->onBeforeMiddleware(function($request) {});

$router->onMiddleware(function($request) {});

$router->onMiddlewareAbort(function($request) {});

$router->onBeforeAction(function($request) {});

$router->onAfterAction(function($request) {});

$router->onBodySent(function($request) {});

$router->onDestroy(function($request) {});

$router->onError(function($request) {});

$router->onRouteMatched(function($request) {});

$router->setDefaultErrorCallback(function($request) {

    return $request->responseMessage();
});

$router->setErrorCallback(404, function() {
  
    return "Page Not Found";
});

// Register your middleware #1.
$router->middleware(function($request) {

});

// Register your middleware #2.
$router->middleware(function($request) {

});



namespace App\Middleware;

class TestMiddleware {

    public function handle($request) {
    
    }
    
}

$router->middleware('App\Middleware\TestMiddleware@handle');

$router->afterMiddleware('App\Middleware\TestMiddleware@handle');

$router->get("/", function($request) {

    return "Hello World";
});



namespace App\Controller;

class MyController {

    public function home($request) {
    
        return "Home";
    }
    
    public function aboutUs($request) {
    
        return "About Us";
    }
    
}

$router->get("/home", "App\Controller\MyController@home");
$router->get("/about-us", "App\Controller\MyController@aboutUs");

$router->get("/members", function($request) {

    return $request->get("per_page");
});

$router->post("/members/{id}", function($request) {

    return $request->post("token");
});

$router->get("/members/{member_id}", function($request) {

    return $request->member_id;
});

$router->abort(503); // Return "Service Unavailable" response.

$router->setHeader("Content-Type", "application/json");

$router->down();

$router->get('/dashboard', 'App\Controller\MyController@dashboard')->down();

$request->method();

$request->uri();

$request->port();

$request->protocol();

$request->secure();

$request->localhost();

$request->xmlHttpRequest();