Classifieds with Kirby

Hey everyone,
I want to create a kind of classifieds with Kirby. It should be possible to register and log in via the frontend. The user can then create and edit ads themselves. Later it will be possible to highlight advertisements for an additional charge.

What is the best way to do this so that I can also see and edit the ads and users in the backend?

These two recipes should help:

http://getkirby.test/docs/cookbook/forms/creating-pages-from-frontend
http://getkirby.test/docs/cookbook/forms/user-registration

Ok, thanks, user registration worked. What is the best way to implement a user area?

I want to redirect the user to their dashboard after login. There he then has the opportunity to edit his profile or create his own pages. What is the best file structure here?

It would be best if the URL were structured like this:
Dashboard: siteurl.de/account
Edit Profile: siteurl.de/account/profil
Create Pages / Edit Pages: siteurl.de/account/my-pages

Since these frontend users are now Kirby users but without access to the Panel, you would have to implement a parent page (e.g. accounts) and make the registered users with this role virtual children of the accounts page: Virtual content | Kirby CMS

Then have a form on the frontend where users can edit their profile and create pages.

If you don’t want to mix virtual content with file-system content, you might want to consider creating those pages in a different parent, e.g. classifieds, where you would have subfolders per user.

Ok, great thanks. Virtual pages work. But how does a form work in virtual pages?

I tried this form (Allow users to change their own data in the frontend - #5 by ErVal) and installed it on a static page and it works perfectly. But if I change the page to a virtual page then the form no longer works because it cannot find the .txt file.

Which text file, sorry, didn’t read through the whole code. And don’t see why are form shouldn’t work, unless you want to store anything in the page without a proper model.

Here is my config.php:

[
            'pattern' => 'account/profile,
            'action'  => function () {
                return Page::factory([
                    'slug' => 'account/profile',
                    'template' => 'account-profile',
                    'content' => [
                        'title' => 'Profile',
                        'uuid'  => Uuid::generate(),
                    ]
                ]);
            }
        ],

This is the account-profile.php template:

<?php dump(get('email')) ?>
  <div class="viewUserData" id="userData">
    <div id="name"><?= $page->username() ?>: <?= $kirby->user()->username() ?></div>
    <div id="email"><?= $page->email() ?>: <?= $kirby->user()->email() ?></div>
  </div>
  <form id="editUserData" class="editUserForm" method="post" action="<?= $page->url() ?>">
    <div>
      <label for="editEmail"><?= $page->username()->html() ?></label>
      <input type="email" id="editEmail" name="editEmail" placeholder="<?= $kirby->user()->email() ?>" value="<?= get('editEmail') ? esc(get('editEmail'), 'attr') : '' ?>">
    </div>
    <div>
      <label for="editPassword"><?= $page->password()->html() ?></label>
      <input type="password" id="editPassword" name="editPassword" value="<?= get('editPassword') ? esc(get('editPassword'), 'attr') : '' ?>">
    </div>
    <div>
      <input type="submit" name="user" value="senden" onsubmit="">
    </div>
  </form>

This is the controller account-profile.php:

<?php
return function ($kirby, $page) {
    $error = "";
    if ($kirby->request()->is('POST') && get('user')) {
        try {
            if(get('editEmail') !== ''){
                $user = $kirby->user()->changeEmail(get('editEmail'));
            }
        } catch (Exception $e) {
            $error = true;
        }
    }
    return [
        'error' => $error
    ];
};

I created the account-profile page as a page in the panel to create the design, and the form worked. Now I have deleted the page because it is supposed to be a virtual page and the form no longer works. Thanks for help.

Please be more specific what that means.

With the code as posted above, you should get errors, because

$page->username() etc is nowhere defined in your content
$kirby->user() will only work if a user is logged in, so you always need to check if a user exists.

And the same is true in your controller, you are assuming a logged-in user, which is ok if a user is already locked in when seeing this page. But is that the case?

Ah now I see the error with the $page is not defined. But how can I solve this?

Yes, the site may only be used by registered users as this is a members’ area.

That is not the problem. The problem is that userName and email don’t seem to be set anywhere. But this should not cause the problem. You should nevertheless check if you have a $kirby->user() before calling any methods, it’s good practice.

But I’m still missing the information what exactly does not work with the form?

But I know what the problem is: Your route only listens to GET requests, while you submit the form with a POST request, so the route will not work and you need to add the method:

		[
			'pattern' => 'account/profile',
			'method' => 'GET|POST',
            'action'  => function () {
                return Page::factory([
                    'slug' => 'account/profile',
                    'template' => 'account-profile',
                    'content' => [
                        'title' => 'Profile',
                        'uuid'  => Uuid::generate(),
                        'username' => 'User name',
                        'email' => 'lala'
                    ]
                ]);
            }
        ],

Thanks, it works now.