Skip to content

Commit 0548366

Browse files
authored
Update README.md
1 parent e256163 commit 0548366

File tree

1 file changed

+60
-75
lines changed

1 file changed

+60
-75
lines changed

README.md

Lines changed: 60 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ The current version requires PHP `v7.1` or newer versions including `v8.0`.
3737
- [Route Groups](#route-groups)
3838
- [Middleware](#middleware)
3939
- [Domains and Subdomains](#domains-and-subdomains)
40-
- [Views](#views)
4140
- [Route Names](#route-names)
4241
- [Current Route](#current-route)
4342
- [Error Handling](#error-handling)
@@ -141,21 +140,11 @@ use MiladRahimi\PhpRouter\Router;
141140
142141
$router = Router::create();
143142
144-
$router->get('/', function () {
145-
return 'GET';
146-
});
147-
$router->post('/', function () {
148-
return 'POST';
149-
});
150-
$router->put('/', function () {
151-
return 'PUT';
152-
});
153-
$router->patch('/', function () {
154-
return 'PATCH';
155-
});
156-
$router->delete('/', function () {
157-
return 'DELETE';
158-
});
143+
$router->get('/', function () { /* ... */ });
144+
$router->post('/', function () { /* ... */ });
145+
$router->put('/', function () { /* ... */ });
146+
$router->patch('/', function () { /* ... */ });
147+
$router->delete('/', function () { /* ... */ });
159148
160149
$router->dispatch();
161150
```
@@ -167,15 +156,9 @@ use MiladRahimi\PhpRouter\Router;
167156

168157
$router = Router::create();
169158

170-
$router->define('GET', '/', function () {
171-
return 'GET';
172-
});
173-
$router->define('OPTIONS', '/', function () {
174-
return 'OPTIONS';
175-
});
176-
$router->define('CUSTOM', '/', function () {
177-
return 'CUSTOM';
178-
});
159+
$router->define('GET', '/', function () { /* ... */ });
160+
$router->define('OPTIONS', '/', function () { /* ... */ });
161+
$router->define('CUSTOM', '/', function () { /* ... */ });
179162

180163
$router->dispatch();
181164
```
@@ -219,7 +202,7 @@ class UsersController
219202
{
220203
function index()
221204
{
222-
return 'Class: UsersController, Method: index';
205+
return 'Class: UsersController & Method: index';
223206
}
224207

225208
function handle()
@@ -254,18 +237,22 @@ $router = Router::create();
254237
$router->get('/post/{id}', function ($id) {
255238
return "The content of post $id";
256239
});
240+
257241
// Optional parameter
258242
$router->get('/welcome/{name?}', function ($name = null) {
259243
return 'Welcome ' . ($name ?: 'Dear User');
260244
});
245+
261246
// Optional parameter, Optional / (Slash)!
262247
$router->get('/profile/?{user?}', function ($user = null) {
263248
return ($user ?: 'Your') . ' profile';
264249
});
250+
265251
// Optional parameter with default value
266252
$router->get('/roles/{role?}', function ($role = 'guest') {
267253
return "Your role is $role";
268254
});
255+
269256
// Multiple parameters
270257
$router->get('/post/{pid}/comment/{cid}', function ($pid, $cid) {
271258
return "The comment $cid of the post $pid";
@@ -286,14 +273,12 @@ $router = Router::create();
286273
// "id" must be numeric
287274
$router->pattern('id', '[0-9]+');
288275

289-
$router->get('/post/{id}', function (int $id) {
290-
return 'Content of the post: ' . $id;
291-
});
276+
$router->get('/post/{id}', function (int $id) { /* ... */ });
292277

293278
$router->dispatch();
294279
```
295280

296-
### Requests and Responses
281+
### Requests, Responses, and Views
297282

298283
PhpRouter uses [laminas-diactoros](https://github.com/laminas/laminas-diactoros/)
299284
(formerly known as [zend-diactoros](https://github.com/zendframework/zend-diactoros))
@@ -363,6 +348,51 @@ $router->get('/redirect', function () {
363348
$router->dispatch();
364349
```
365350

351+
#### Views
352+
353+
You might need to create a classic-style website that uses views.
354+
PhpRouter has a simple feature for working with PHP/HTML views.
355+
Look at the following example.
356+
357+
```php
358+
use MiladRahimi\PhpRouter\Router;
359+
use MiladRahimi\PhpRouter\View\View
360+
361+
$router = Router::create();
362+
363+
// Setup view feature and set the directory of view files
364+
$router->setupView(__DIR__ . '/../views');
365+
366+
$router->get('/profile', function (View $view) {
367+
// It looks for a view with path: __DIR__/../views/profile.phtml
368+
return $view->make('profile', ['user' => 'Jack']);
369+
});
370+
371+
$router->get('/blog/post', function (View $view) {
372+
// It looks for a view with path: __DIR__/../views/blog/post.phtml
373+
return $view->make('blog.post', ['post' => $post]);
374+
});
375+
376+
$router->dispatch();
377+
```
378+
379+
There is also some points:
380+
* View files must have the ".phtml" extension (e.g. `profile.phtml`).
381+
* You must separate sub-directories with `.` (e.g. `blog.post` for `blog/post.phtml`).
382+
383+
View files are pure PHP or mixed with HTML.
384+
You should use PHP language with template style in the view files.
385+
This is a sample view file:
386+
387+
```php
388+
<h1><?php echo $title ?></h1>
389+
<ul>
390+
<?php foreach ($posts as $post): ?>
391+
<li><?php echo $post['content'] ?></li>
392+
<?php endforeach ?>
393+
</ul>
394+
```
395+
366396
### Route Groups
367397

368398
You can categorize routes into groups.
@@ -492,51 +522,6 @@ $router->group(['domain' => '(.*).example.com'], function(Router $router) {
492522
$router->dispatch();
493523
```
494524

495-
### Views
496-
497-
You might need to create a classic-style website that uses views.
498-
PhpRouter has a simple feature for working with PHP/HTML views.
499-
Look at the following example.
500-
501-
```php
502-
use MiladRahimi\PhpRouter\Router;
503-
use MiladRahimi\PhpRouter\View\View
504-
505-
$router = Router::create();
506-
507-
// Setup view feature and set the directory of view files
508-
$router->setupView(__DIR__ . '/../views');
509-
510-
$router->get('/profile', function (View $view) {
511-
// It looks for a view with path: __DIR__/../views/profile.phtml
512-
return $view->make('profile', ['user' => 'Jack']);
513-
});
514-
515-
$router->get('/blog/post', function (View $view) {
516-
// It looks for a view with path: __DIR__/../views/blog/post.phtml
517-
return $view->make('blog.post', ['post' => $post]);
518-
});
519-
520-
$router->dispatch();
521-
```
522-
523-
There is also some points:
524-
* View files must have the ".phtml" extension (e.g. `profile.phtml`).
525-
* You must separate sub-directories with `.` (e.g. `blog.post` for `blog/post.phtml`).
526-
527-
View files are pure PHP or mixed with HTML.
528-
You should use PHP language with template style in the view files.
529-
This is a sample view file:
530-
531-
```php
532-
<h1><?php echo $title ?></h1>
533-
<ul>
534-
<?php foreach ($posts as $post): ?>
535-
<li><?php echo $post['content'] ?></li>
536-
<?php endforeach ?>
537-
</ul>
538-
```
539-
540525
### Route Names
541526

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

0 commit comments

Comments
 (0)