diff --git a/demo2/README.md b/demo2/README.md new file mode 100644 index 0000000..a6c5be0 --- /dev/null +++ b/demo2/README.md @@ -0,0 +1,28 @@ +# demo2 + +This is a demonstration featuring the actual implementation of routers and controllers using bramus/router and symfony/http-foundation. + +## example routes + +- http://localhost:8080/ +- http://localhost:8080/action1 + +- http://localhost:8080/api/v1/action2 +- http://localhost:8080/no-actioin-found + +## Installation + +Installation is possible using Composer + +``` +cd .\demo2\ + +composer install +composer dump-autoload + + +php -S localhost:8080 + +``` + +An additional demonstration, labeled as 'demo2,' can be found within the 'demo2' subfolder. You can showcase it either through your preferred web server or by utilizing PHP 5.4 or later's built-in server with the command php -S localhost:8080 in the shell. \ No newline at end of file diff --git a/demo2/composer.json b/demo2/composer.json new file mode 100644 index 0000000..c8cc153 --- /dev/null +++ b/demo2/composer.json @@ -0,0 +1,12 @@ +{ + "require": { + "bramus/router": "~1.6", + "symfony/http-foundation": "^5.4" + }, + "autoload": { + "psr-4": {"App\\": "src/"}, + "exclude-from-classmap": ["/Tests/"] + }, + "require-dev": { + } +} diff --git a/demo2/index.php b/demo2/index.php new file mode 100644 index 0000000..1cde7fc --- /dev/null +++ b/demo2/index.php @@ -0,0 +1,7 @@ +routes()->run(); \ No newline at end of file diff --git a/demo2/src/Controller/Example.php b/demo2/src/Controller/Example.php new file mode 100644 index 0000000..fd0f73a --- /dev/null +++ b/demo2/src/Controller/Example.php @@ -0,0 +1,64 @@ +headers->set('Content-Type', 'text/plain'); + $response->setStatusCode(Response::HTTP_OK); + + return $response->send(); + } + + /** + * @return JsonResponse with json + */ + public function action2() + { + // Create an associative array representing the data to be encoded as JSON + $data = [ + 'message' => 'Hello, this is a JSON response example from /api/v1/action2 route!', + 'status' => 'success', + ]; + + // Use JsonResponse to create a JSON response with the provided data + $response = new JsonResponse($data); + + // Optionally, you can customize the response, e.g., set additional headers + + return $response->send(); + } + + /** + * @return Response with plain text for no matching route found! + */ + public function notFoundAction() + { + // Create a plain text Response for no matching route found + $response = new Response('404 Not Found - No matching route found!'); + + // Set the status code to 404 + $response->setStatusCode(Response::HTTP_NOT_FOUND); + + // Optionally, you can customize other properties of the response + $response->headers->set('Content-Type', 'text/plain'); + + return $response->send(); + } + +} diff --git a/demo2/src/Router.php b/demo2/src/Router.php new file mode 100644 index 0000000..282bb23 --- /dev/null +++ b/demo2/src/Router.php @@ -0,0 +1,49 @@ +router = new \Bramus\Router\Router(); + $this->router->setNamespace('\App\Controller'); + } + + /** + * manage all your routes here + * Route example + * + * http://localhost:8080 + * http://localhost:8080/action1 + * + * http://localhost:8080/api/v1/action2 + * + * http://localhost:8080/no-action-found + */ + public function routes() + { + ////////////Declare the routes here////////////// + + //Use a shorter local variable name + $rout = &$this->router; + + + //without prefix + $rout->get('/', 'Example@action1'); + $rout->get('/action1', 'Example@action1'); + + //route with prefix + $rout->mount('/api/v1', function () use ($rout) { + $rout->get('action2', 'Example@action2'); + }); + + //if nothing matches + $rout->set404('/api(/.*)?', 'Example@notFoundAction'); + + //finally return the route + return $rout; + } +}