Skip to content

Commit a66f1c6

Browse files
authored
Update README.md
1 parent f0c5037 commit a66f1c6

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

README.md

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ Some of the provided features:
1616
* Route groups (by prefix, middleware, and domain)
1717
* Route naming (and generating route by name)
1818
* PSR-7 requests and responses
19+
* Views
1920
* Multiple (sub)domains (using regex patterns)
2021
* Custom HTTP methods
21-
* Integrated with a [PhpContainer](https://github.com/miladrahimi/phpcontainer)
22+
* Integrated with an Ioc Container([PhpContainer](https://github.com/miladrahimi/phpcontainer))
2223
* Method and constructor auto-injection of request, route, url, etc
2324

2425
The current version requires PHP `v7.1` or newer versions including (`v8.0`).
@@ -36,6 +37,7 @@ The current version requires PHP `v7.1` or newer versions including (`v8.0`).
3637
- [Route Groups](#route-groups)
3738
- [Middleware](#middleware)
3839
- [Domains and Subdomains](#domains-and-subdomains)
40+
- [Views](#views)
3941
- [Route Names](#route-names)
4042
- [Current Route](#current-route)
4143
- [Error Handling](#error-handling)
@@ -102,17 +104,36 @@ Here you can see sample configurations for NGINX and Apache HTTP Server.
102104
103105
It's so easy to work with PhpRouter! Just take a look at the following example.
104106
105-
```php
106-
use MiladRahimi\PhpRouter\Router;
107+
* API Example:
107108
108-
$router = Router::create();
109+
```php
110+
use MiladRahimi\PhpRouter\Router;
111+
use Laminas\Diactoros\Response\JsonResponse;
109112
110-
$router->get('/', function () {
111-
return 'This is homepage!';
112-
});
113+
$router = Router::create();
113114
114-
$router->dispatch();
115-
```
115+
$router->get('/', function () {
116+
return new JsonResponse(['message' => 'ok']);
117+
});
118+
119+
$router->dispatch();
120+
```
121+
122+
* View Example:
123+
124+
```php
125+
use MiladRahimi\PhpRouter\Router;
126+
use MiladRahimi\PhpRouter\View\View
127+
128+
$router = Router::create();
129+
$router->setupView('/../views');
130+
131+
$router->get('/', function (View $view) {
132+
return $view->make('profile', ['user' => 'Jack']);
133+
});
134+
135+
$router->dispatch();
136+
```
116137
117138
### HTTP Methods
118139
@@ -474,6 +495,38 @@ $router->group(['domain' => '(.*).example.com'], function(Router $router) {
474495
$router->dispatch();
475496
```
476497

498+
### Views
499+
500+
You might need to create a classic-style website that uses views.
501+
PhpRouter has a simple feature for working with PHP/HTML views.
502+
Look at the following example.
503+
504+
```php
505+
use MiladRahimi\PhpRouter\Router;
506+
use MiladRahimi\PhpRouter\View\View
507+
508+
$router = Router::create();
509+
510+
// Setup view feature and set the directory of view files
511+
$router->setupView(__DIR__ . '/../views');
512+
513+
$router->get('/profile', function (View $view) {
514+
// It looks for a view with path: __DIR__/../views/profile.phtml
515+
return $view->make('profile', ['user' => 'Jack']);
516+
});
517+
518+
$router->get('/blog/post', function (View $view) {
519+
// It looks for a view with path: __DIR__/../views/blog/post.phtml
520+
return $view->make('blog.post', ['user' => 'Jack']);
521+
});
522+
523+
$router->dispatch();
524+
```
525+
526+
There is also some points:
527+
* View files must have the ".phtml" extension (e.g. `profile.phtml`).
528+
* You must separate sub-directories with `.` (e.g. `blog.post` for `blog/post.phtml`).
529+
477530
### Route Names
478531

479532
You can assign names to your routes and use them in your codes instead of the hard-coded URLs.

0 commit comments

Comments
 (0)