Update 10-dynamic-pages.md
This commit is contained in:
parent
215c07c94a
commit
57f6118308
1 changed files with 3 additions and 21 deletions
|
@ -48,7 +48,7 @@ namespace Example\Page;
|
||||||
|
|
||||||
interface PageReader
|
interface PageReader
|
||||||
{
|
{
|
||||||
public function readBySlug($slug) : string;
|
public function readBySlug(string $slug) : string;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ class FilePageReader implements PageReader
|
||||||
$this->pageFolder = $pageFolder;
|
$this->pageFolder = $pageFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function readBySlug($slug) : string
|
public function readBySlug(string $slug) : string
|
||||||
{
|
{
|
||||||
return 'I am a placeholder';
|
return 'I am a placeholder';
|
||||||
}
|
}
|
||||||
|
@ -142,18 +142,7 @@ class Page
|
||||||
|
|
||||||
So far so good, now let's make our `FilePageReader` actually do some work.
|
So far so good, now let's make our `FilePageReader` actually do some work.
|
||||||
|
|
||||||
Again, let's check first that the proper type was passed into the method:
|
We need to be able to communicate that a page was not found. For this we can create a custom exception that we can catch later. In your `src/Page` folder, create a `InvalidPageException.php` file with this content:
|
||||||
|
|
||||||
```php
|
|
||||||
public function readBySlug($slug)
|
|
||||||
{
|
|
||||||
if (!is_string($slug)) {
|
|
||||||
throw new InvalidArgumentException('slug must be a string');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
We also need to be able to communicate that a page was not found. For this we can create a custom exception that we can catch later. In your `src/Page` folder, create a `InvalidPageException.php` file with this content:
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php declare(strict_types = 1);
|
<?php declare(strict_types = 1);
|
||||||
|
@ -207,13 +196,6 @@ public function show($params)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Add this at the top of your file:
|
|
||||||
```php
|
|
||||||
use Example\Page\InvalidPageException;
|
|
||||||
```
|
|
||||||
|
|
||||||
It is important that you don't forget this step, otherwise it will try to catch the wrong exception (it's looking in the wrong namespace) and thus will never catch it.
|
|
||||||
|
|
||||||
Try a few different URLs to check that everything is working as it should. If something is wrong, go back and debug it until it works.
|
Try a few different URLs to check that everything is working as it should. If something is wrong, go back and debug it until it works.
|
||||||
|
|
||||||
And as always, don't forget to commit your changes.
|
And as always, don't forget to commit your changes.
|
||||||
|
|
Loading…
Reference in a new issue