You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$router->get('/blog/post/{id}', function (int $id) {
252
+
return 'Content of the post: ' . $id;
253
+
});
262
254
263
255
$router->dispatch();
264
256
```
265
257
266
258
## HTTP Request and Request
267
259
268
-
PhpRouter uses [zend-diactoros](https://github.com/zendframework/zend-diactoros) package (version 2) to provide [PSR-7](https://www.php-fig.org/psr/psr-7) complaint request and response objects to your controllers and middleware.
260
+
PhpRouter uses [zend-diactoros](https://github.com/zendframework/zend-diactoros) package (v2) to provide [PSR-7](https://www.php-fig.org/psr/psr-7) complaint request and response objects to your controllers and middleware.
269
261
270
262
### Request
271
263
@@ -291,8 +283,8 @@ $router->get('/', function (ServerRequest $request) {
291
283
]);
292
284
});
293
285
294
-
$router->post('/blog/posts', function (ServerRequest $request) {
295
-
$post = new \App\Models\Post();
286
+
$router->post('/posts', function (ServerRequest $request) {
return new HtmlResponse('<html>This is also an HTML response</html>', 200);
325
317
})
326
318
->get('/json', function () {
327
-
return new JsonResponse(['message' => 'Unauthorized!'], 401);
319
+
return new JsonResponse(['error' => 'Unauthorized!'], 401);
328
320
})
329
321
->get('/text', function () {
330
322
return new TextResponse('This is a plain text...');
@@ -334,7 +326,6 @@ $router
334
326
});
335
327
336
328
$router->dispatch();
337
-
338
329
```
339
330
340
331
#### Redirection Response
@@ -380,7 +371,7 @@ interface Middleware
380
371
*
381
372
* @param ServerRequestInterface $request
382
373
* @param Closure $next
383
-
* @return ResponseInterface|mixed
374
+
* @return ResponseInterface|mixed|null
384
375
*/
385
376
public function handle(ServerRequestInterface $request, Closure $next);
386
377
}
@@ -394,8 +385,8 @@ it passes the request to the next middleware or the controller (if there is no m
394
385
```php
395
386
use MiladRahimi\PhpRouter\Router;
396
387
use MiladRahimi\PhpRouter\Middleware;
397
-
use Psr\Http\Message\ResponseInterface;
398
388
use Psr\Http\Message\ServerRequestInterface;
389
+
use Zend\Diactoros\Response\JsonResponse;
399
390
400
391
class AuthMiddleware implements Middleware
401
392
{
@@ -405,22 +396,24 @@ class AuthMiddleware implements Middleware
405
396
return $next($request);
406
397
}
407
398
408
-
return new EmptyResponse(401);
399
+
return new JsonResponse(['error' => 'Unauthorized!'], 401);
409
400
}
410
401
}
411
402
412
403
$router = new Router();
413
404
414
-
$router->get('/auth', function () { return 'OK' }, AuthMiddleware::class);
405
+
$router->get('/admin', function () {
406
+
return 'This is admin panel!';
407
+
}, AuthMiddleware::class);
415
408
416
409
$router->dispatch();
417
410
```
418
411
419
412
Middleware can be implemented using closures but it doesn’t make scense to do so!
420
413
421
-
## Domain and Subdomain
414
+
## Domain and Sub-domain
422
415
423
-
Your application may serve different services on different domains/subdomains or it may assign subdomain dynamically to users or services. In this case, you need to specify domain or subdomain in addition to the URIs in your routes.
416
+
Your application may serve different services on different domains/subdomains or it may assign sub-domain dynamically to users or services. In this case, you need to specify domain or sub-domain in addition to the URIs in your routes.
Notice that domain parameter receives a regex pattern not a simple string.
443
-
444
435
## Route Groups
445
436
446
-
Usually routes can fit in a groups that have common attributes like middleware, domain/subdomain and prefix. To group routes you can follow the example below.
437
+
Usually routes can fit in a groups that have common attributes like middleware, domain/sub-domain and prefix. To group routes you can follow the example below.
447
438
448
439
```php
440
+
use MiladRahimi\PhpRouter\Examples\Samples\SimpleMiddleware;
449
441
use MiladRahimi\PhpRouter\Router;
450
442
451
443
$router = new Router();
452
444
445
+
// A group with uri prefix
453
446
$router->group(['prefix' => '/admin'], function (Router $router) {
0 commit comments