Skip to content

actual implementation of routers and controllers using bramus/router #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions demo2/README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions demo2/composer.json
Original file line number Diff line number Diff line change
@@ -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": {
}
}
7 changes: 7 additions & 0 deletions demo2/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
ini_set('display_errors','Off');
use App\Router;

require __DIR__ . '/vendor/autoload.php';

(new Router())->routes()->run();
64 changes: 64 additions & 0 deletions demo2/src/Controller/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class Example
{

/**
* @return Response with plain text
*/
public function action1()
{

// Create a Response object with a simple text content
$content = 'Hello, this is a plain text example from / or /action1 routes!';
$response = new Response($content);

// Customize the response, if needed
$response->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();
}

}
49 changes: 49 additions & 0 deletions demo2/src/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App;

class Router
{
protected $router;
public function __construct()
{
// Create Router instance
$this->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;
}
}