Skip to content
Merged
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
24 changes: 21 additions & 3 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,27 @@ you keep adding more controllers to a single application.
For this reason, we recommend using [controller classes](../best-practices/controllers.md)
for production use-cases like this:

```php title="public/index.php"
$app->get('/', new Acme\Todo\HelloController());
```
=== "Using controller instances"

```php title="public/index.php"
<?php

// …

$app->get('/', new Acme\Todo\HelloController());
```

=== "Using controller names"

```php title="public/index.php"
<?php

// …

$app->get('/', Acme\Todo\HelloController::class);
```

<!-- -->

```php title="src/HelloController.php"
<?php
Expand Down
152 changes: 115 additions & 37 deletions docs/api/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,32 @@ class DemoMiddleware
}
}
```
```php title="public/index.php"
<?php

use Acme\Todo\DemoMiddleware;
use Acme\Todo\UserController;
=== "Using middleware instances"

```php title="public/index.php"
<?php

// …
use Acme\Todo\DemoMiddleware;
use Acme\Todo\UserController;

$app->get('/user', new DemoMiddleware(), new UserController());
```
// …

$app->get('/user', new DemoMiddleware(), new UserController());
```

=== "Using middleware names"

```php title="public/index.php"
<?php

use Acme\Todo\DemoMiddleware;
use Acme\Todo\UserController;

// …

$app->get('/user', DemoMiddleware::class, UserController::class);
```

This highlights how middleware classes provide the exact same functionaly as using inline functions,
yet provide a cleaner and more reusable structure.
Expand Down Expand Up @@ -145,17 +161,31 @@ class UserController
}
```

```php
# public/index.php
<?php
=== "Using middleware instances"

```php title="public/index.php"
<?php

use Acme\Todo\AdminMiddleware;
use Acme\Todo\UserController;
use Acme\Todo\AdminMiddleware;
use Acme\Todo\UserController;

// …
// …

$app->get('/user', new AdminMiddleware(), new UserController());
```
$app->get('/user', new AdminMiddleware(), new UserController());
```

=== "Using middleware names"

```php title="public/index.php"
<?php

use Acme\Todo\AdminMiddleware;
use Acme\Todo\UserController;

// …

$app->get('/user', AdminMiddleware::class, UserController::class);
```

For example, an HTTP `GET` request for `/user` would first call the middleware handler which then modifies this request and passes the modified request to the next controller function.
This is commonly used for HTTP authentication, login handling and session handling.
Expand Down Expand Up @@ -210,16 +240,31 @@ class UserController
}
```

```php title="public/index.php"
<?php
=== "Using middleware instances"

```php title="public/index.php"
<?php

use Acme\Todo\ContentTypeMiddleware;
use Acme\Todo\UserController;
use Acme\Todo\ContentTypeMiddleware;
use Acme\Todo\UserController;

// …
// …

$app->get('/user', new ContentTypeMiddleware(), new UserController());
```
$app->get('/user', new ContentTypeMiddleware(), new UserController());
```

=== "Using middleware names"

```php title="public/index.php"
<?php

use Acme\Todo\ContentTypeMiddleware;
use Acme\Todo\UserController;

// …

$app->get('/user', ContentTypeMiddleware::class, UserController::class);
```

For example, an HTTP `GET` request for `/user` would first call the middleware handler which passes on the request to the controller function and then modifies the response that is returned by the controller function.
This is commonly used for cache handling and response body transformations (compression etc.).
Expand Down Expand Up @@ -428,17 +473,33 @@ a response object synchronously:
}
```

<!-- -->

```php title="public/index.php"
<?php
=== "Using middleware instances"

use Acme\Todo\AsyncContentTypeMiddleware;
use Acme\Todo\AsyncUserController;
```php title="public/index.php"
<?php

// …
use Acme\Todo\AsyncContentTypeMiddleware;
use Acme\Todo\AsyncUserController;

$app->get('/user', new AsyncContentTypeMiddleware(), new AsyncUserController());
```
// …

$app->get('/user', new AsyncContentTypeMiddleware(), new AsyncUserController());
```

=== "Using middleware names"

```php title="public/index.php"
<?php

use Acme\Todo\AsyncContentTypeMiddleware;
use Acme\Todo\AsyncUserController;

// …

$app->get('/user', AsyncContentTypeMiddleware::class, AsyncUserController::class);
```

For example, an HTTP `GET` request for `/user` would first call the middleware handler which passes on the request to the controller function and then modifies the response that is returned by the controller function.
This is commonly used for cache handling and response body transformations (compression etc.).
Expand All @@ -456,18 +517,35 @@ This is commonly used for cache handling and response body transformations (comp
Additionally, you can also add middleware to the [`App`](app.md) object itself
to register a global middleware handler:

```php hl_lines="7" title="public/index.php"
<?php
=== "Using middleware instances"

use Acme\Todo\AdminMiddleware;
use Acme\Todo\UserController;
```php hl_lines="6" title="public/index.php"
<?php

$app = new FrameworkX\App(new AdminMiddleware());
use Acme\Todo\AsyncContentTypeMiddleware;
use Acme\Todo\AsyncUserController;

$app->get('/user', new UserController());
$app = new FrameworkX\App(new AdminMiddleware());

$app->run();
```
$app->get('/user', new UserController());

$app->run();
```

=== "Using middleware names"

```php hl_lines="6" title="public/index.php"
<?php

use Acme\Todo\AsyncContentTypeMiddleware;
use Acme\Todo\AsyncUserController;

$app = new FrameworkX\App(AdminMiddleware::class);

$app->get('/user', UserController::class);

$app->run();
```

Any global middleware handler will always be called for all registered routes
and also any requests that can not be routed.
Expand Down
35 changes: 27 additions & 8 deletions docs/best-practices/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,37 @@ For real-world applications, we highly recommend structuring your application
into individual controller classes. This way, we can break up the above
definition into three even simpler files:

```php title="public/index.php"
<?php
=== "Using controller instances"

require __DIR__ . '/../vendor/autoload.php';
```php title="public/index.php"
<?php

$app = new FrameworkX\App();
require __DIR__ . '/../vendor/autoload.php';

$app->get('/', new Acme\Todo\HelloController());
$app->get('/users/{name}', new Acme\Todo\UserController());
$app = new FrameworkX\App();

$app->run();
```
$app->get('/', new Acme\Todo\HelloController());
$app->get('/users/{name}', new Acme\Todo\UserController());

$app->run();
```

=== "Using controller names"

```php title="public/index.php"
<?php

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

$app = new FrameworkX\App();

$app->get('/', Acme\Todo\HelloController::class);
$app->get('/users/{name}', Acme\Todo\UserController::class);

$app->run();
```

<!-- -->

```php title="src/HelloController.php"
<?php
Expand Down
Loading