Requests in controllers not working with templates within a plugin?

I am trying to figure out how to use controllers within a plugin.
For a start I figured I’d take the frontend login recipe from here
and simply apply it on a plugin.

And it seems like the controller works with the plugin in a way (at least I don’t get an “$error is undefined” and I can do stuff within the controller that affect whatever goes on in the template)

However, when I try to login using the provided form, clicking on submit reloads the page but without the form this time…
Going to the panel reveals that the login did not work.
I tried to throw an error in case the request was recognized at all, but that did nothing…

Here’s what I have so far:

pluginfolder/templates/login.php:

<?php snippet('header') ?>
<?php snippet('menu') ?>

<section class="viewLogin">
    <h1><?= $page->title()->html() ?></h1>

    <?php if($error): ?>
    <div class="alert">es ist ein Fehler vorgekommen</div>
    <?php endif ?>


    <form class="loginform" method="post" action="<?= $page->url() ?>">
      <div class="logininput">
        <label for="email">E-Mail</label>
        <input type="email" id="email" name="email" value="<?= get('email') ? esc(get('email'), 'attr') : '' ?>">
      </div>
      <div class="logininput">
        <label for="password">Passwort</label>
        <input type="password" id="password" name="password" value="<?= get('password') ? esc(get('password'), 'attr') : '' ?>">
      </div>
      <div><?= $page->passwordreset()->kt() ?></div>
      <div class="loginbutton">
        <input type="submit" name="login" value="Login">
      </div>
    </form>

  </section>


<?php snippet('footer') ?>

pluginfolder/controllers/login.php:

<?php

return function ($kirby) {

    $user = $kirby->user();
    // don't show the login screen to already logged in users
    if ($user) {
        go('/');
    }

    $error = false;

    // handle the form submission
    if ($kirby->request()->is('POST') && get('login')) {
        // try to log the user in with the provided credentials
        try {

            $kirby->auth()->login(get('email'), get('password'));
            go('/somewhereelse');

        }catch (Exception $e) {
        $error = true;
        }

    }

    return [
        'error' => $error
    ];

};

and finally my pluginfolder/index.php:

'routes' => [
        [
          'pattern' => 'login',
          'action'  => function () {
                return Page::factory([
                    'slug' => 'login',
                    'template' => 'login',
                    'model' => 'login',
                    'content' => [
                        'title' => 'Login',
                    ]
                ]);
          }
        ],
...
	'controllers' => [
        'login' => require __DIR__ . "/controllers/login.php"
	],
	'templates' => [
        'login' => __DIR__ . '/templates/login.php'
]

Am I missing something?

The problem is that your route only listens to a get request, add

'pattern' => 'login',
'method' => 'GET|POST',
'action'  => function () {//...}
1 Like

So that was what I was missing!

Thank you for taking the time to help me out.