Skip to content

Commit c52d2c6

Browse files
miladrahimiMilad Rahimi
authored andcommitted
add publish method and add example dir
1 parent ff2057e commit c52d2c6

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

example/index.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Milad Rahimi <[email protected]>
5+
* Date: 7/13/2018 AD
6+
* Time: 18:33
7+
*/
8+
9+
use MiladRahimi\Router\Exceptions\RouteNotFoundException;
10+
use MiladRahimi\Router\Router;
11+
use Zend\Diactoros\Response\EmptyResponse;
12+
use Zend\Diactoros\Response\HtmlResponse;
13+
use Zend\Diactoros\Response\JsonResponse;
14+
use Zend\Diactoros\Response\RedirectResponse;
15+
use Zend\Diactoros\Response\TextResponse;
16+
use Zend\Diactoros\ServerRequest;
17+
18+
require './../vendor/autoload.php';
19+
20+
$router = new Router();
21+
22+
$router->get('/', function (ServerRequest $request) {
23+
return new JsonResponse([
24+
'method' => $request->getMethod(),
25+
'uri' => $request->getUri(),
26+
'body' => $request->getBody(),
27+
'headers' => $request->getHeaders(),
28+
]);
29+
});
30+
31+
$router->post('/', function () {
32+
$html = '<html>Object successfully created.</html>';
33+
34+
return new HtmlResponse($html, 201);
35+
});
36+
37+
$router->put('/{id}', function ($id) {
38+
$text = 'The entity with ' . $id . ' updated.';
39+
40+
return new TextResponse($text);
41+
});
42+
43+
$router->patch('/{id}', function () {
44+
return new EmptyResponse();
45+
});
46+
47+
$router->get('/redirect', function () {
48+
return new RedirectResponse('https://miladrahimi.com');
49+
});
50+
51+
try {
52+
$router->dispatch();
53+
} catch (RouteNotFoundException $e) {
54+
$router->publish(new EmptyResponse(404));
55+
} catch (Throwable $e) {
56+
echo '<pre>' . print_r($e, true) . '</pre>';
57+
}

src/MiladRahimi/Router/Router.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ public function map(
188188
/**
189189
* Dispatch routes and run the application
190190
*
191+
* @throws InvalidControllerException
192+
* @throws InvalidMiddlewareException
193+
* @throws RouteNotFoundException
191194
* @throws Throwable
192195
*/
193196
public function dispatch()
@@ -211,7 +214,7 @@ public function dispatch()
211214
RouteAttributes::URI => $routeAttributes[RouteAttributes::URI],
212215
];
213216

214-
$this->publisher->publish($this->run($routeAttributes, $routeParameters));
217+
$this->publish($this->run($routeAttributes, $routeParameters));
215218

216219
return;
217220
}
@@ -226,6 +229,8 @@ public function dispatch()
226229
* @param array $routeAttributes
227230
* @param array $routeParameters
228231
* @return mixed|ResponseInterface
232+
* @throws InvalidControllerException
233+
* @throws InvalidMiddlewareException
229234
* @throws Throwable
230235
*/
231236
private function run(array $routeAttributes, array $routeParameters)
@@ -284,6 +289,7 @@ private function runControllerThroughMiddleware(array $middleware, Closure $cont
284289
* @param Closure|callable|string $controller
285290
* @param array $parameters
286291
* @return ResponseInterface|null
292+
* @throws InvalidControllerException
287293
* @throws Throwable
288294
*/
289295
private function runController($controller, array $parameters)
@@ -525,7 +531,7 @@ public function patch(
525531
*
526532
* @param string $route
527533
* @param Closure|callable|string $controller
528-
* @param \MiladRahimi\Router\Services\Middleware|Middleware[] $middleware
534+
* @param Middleware|Middleware[] $middleware
529535
* @param string|null $domain
530536
* @param string|null $name
531537
*/
@@ -613,6 +619,16 @@ public function currentRouteName()
613619
return null;
614620
}
615621

622+
/**
623+
* Publish http response manually
624+
*
625+
* @param $httpResponse
626+
*/
627+
public function publish($httpResponse)
628+
{
629+
$this->publisher->publish($httpResponse);
630+
}
631+
616632
/**
617633
* Get current http request instance
618634
*

0 commit comments

Comments
 (0)