PHP code example of ride / lib-mvc

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

    

ride / lib-mvc example snippets




use ride\library\http\Header;
use ride\library\http\HttpFactory;
use ride\library\http\Response;
use ride\library\mvc\dispatcher\GenericDispatcher;
use ride\library\mvc\message\Message;
use ride\library\router\GenericRouter;
use ride\library\router\RouteContainer;
use ride\library\router\Route;

// prepare some routes
$route = new Route('/', 'testAction');
$route->setIsDynamic(true);

$routeContainer = new RouteContainer();
$routeContainer->setRoute($route);

// get the request and response
$httpFactory = new HttpFactory();
$httpFactory->setRequestClass('ride\\library\\mvc\\Request');
$httpFactory->setResponseClass('ride\\library\\mvc\\Response');

$request = $httpFactory->createRequestFromServer();
$response = $httpFactory->createResponse();

// route the request
$router = new GenericRouter($routeContainer);
$routerResult = $router->route($request->getMethod(), $request->getBasePath(), $request->getBaseUrl());

// dispatch the route
$returnValue = null;

if ($routerResult->isEmpty()) {
    $response->setStatusCode(Response::STATUS_CODE_NOT_FOUND);
} else {
    $route = $routerResult->getRoute();
    if ($route) {
        $request->setRoute($route);

        $dispatcher = new GenericDispatcher();
        $dispatcher->dispatch($request, $response);
    } else {
        $allowedMethods = $routerResult->getAllowedMethods();

        $response->setStatusCode(Response::STATUS_CODE_METHOD_NOT_ALLOWED);
        $response->addHeader(Header::HEADER_ALLOW, implode(', ', $allowedMethods));
    }
}

// send the response
$response->send($request);

// the test action
function testAction() {
    global $response;

    $response->addMessage(new Message('This is a test action', Message::TYPE_WARNING));
    $response->setBody('test: ' . var_export(func_get_args(), true));
}