Skip to content

Commit

Permalink
fix blog
Browse files Browse the repository at this point in the history
  • Loading branch information
pamparam83 committed Feb 23, 2024
1 parent 9bf304b commit d7828c8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
25 changes: 18 additions & 7 deletions blog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,41 @@ It's intended to show and test all Yii features.
You'll need at least PHP 8.1.

1. Clone this repository.
2. Run `composer install` in your project root directory.
2. Run command in your project root directory.
```bash
composer install
```
3. Run `./yii serve` (on Windows `yii serve`). The application will be started on http://localhost:8080/.
```bash
./yii serve
```
4. Go to the index page. Cycle ORM will create tables, indexes and relations automatically in the configured DB for you.
If you want to disable this behavior then comment out the line with the `Generator\SyncTables::class` in the `config/packges/yiisoft/yii-cycle/params.php`.
In this case you should create migrations to sync changes that you have made to entities with the DB.
5. Run `./yii fixture/add 20` to create some random data.

5. Run command to create some random data.
```bash
./yii fixture/add 20
```
## Console

Console works out of the box and could be executed with `./yii`.

Some commands:

```bash
user/create <login> <password>
fixture/add [count]
./yii user/create login password
./yii fixture/add 10
```

In order to register your own commands, add them to `console/params.php`, `console``commands` section.

## Web application

In order to run the web application, you can either use the built-in web server by running `./yii serve` or you could use a
real web server by pointing it to `/public/index.php`.
In order to run the web application, you can either use the built-in web server by running
```bash
./yii serve
```
or you could use a real web server by pointing it to `/public/index.php`.

More routes could be added by editing `src/Factory/AppRouterFactory`.

Expand Down
2 changes: 1 addition & 1 deletion blog/src/Blog/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function index(
$dataReader = $postRepository->findAllPreloaded();
$paginator = (new OffsetPaginator($dataReader))
->withPageSize(self::POSTS_PER_PAGE)
->withCurrentPage($pageNum);
->withCurrentPage((int)$pageNum);

$data = [
'paginator' => $paginator,
Expand Down
12 changes: 5 additions & 7 deletions blog/src/Blog/Post/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function add(Request $request, FormHydrator $formHydrator): Response

if ($request->getMethod() === Method::POST) {
$form = new PostForm();
if ($formHydrator->populate($form, $parameters['body']) && $form->isValid()) {
if ($formHydrator->populateAndValidate($form, $parameters['body'])) {
$this->postService->savePost($this->userService->getUser(), new Post(), $form);

return $this->webService->getRedirectResponse('blog/index');
Expand All @@ -65,7 +65,8 @@ public function edit(
Request $request,
PostRepository $postRepository,
ValidatorInterface $validator,
CurrentRoute $currentRoute
CurrentRoute $currentRoute,
FormHydrator $formHydrator
): Response {
$slug = $currentRoute->getArgument('slug');
$post = $postRepository->fullPostPage($slug);
Expand All @@ -87,16 +88,13 @@ public function edit(
if ($request->getMethod() === Method::POST) {
$form = new PostForm();
$body = $request->getParsedBody();
if ($form->load($body) && $validator
->validate($form)
->isValid()) {
if ($formHydrator->populateAndValidate($form,$body)) {
$this->postService->savePost($this->userService->getUser(), $post, $form);

return $this->webService->getRedirectResponse('blog/index');
}

$parameters['body'] = $body;
$parameters['errors'] = $form->getFormErrors();
$parameters['errors'] = $form->getValidationResult()->getErrorMessagesIndexedByAttribute();
}

return $this->viewRenderer->render('__form', $parameters);
Expand Down

0 comments on commit d7828c8

Please sign in to comment.