There are two ways to load a form using a route. You can either load up a callback that loads a form and returns it as part of the build array, or you can load up the form directly by setting the _form parameter under defaults.
You can search the codebase to find working examples, copy them into your mymodule.routing.yml, edit them to your needs and then rebuild the cache.
Loading form from callback:
There's a working example in the contact module:
/core/modules/contact/contact.routing.yml
entity.user.contact_form:
path: '/user/{user}/contact'
defaults:
_title: 'Contact'
_controller: '\Drupal\contact\Controller\ContactController::contactPersonalPage'
requirements:
_access_contact_personal_tab: 'TRUE'
user: \d+
Then in /core/modules/contact/src/Controller/ContactController.php
you can see an example of how to load a form in the callback:
public function contactPersonalPage(UserInterface $user) {
// Do not continue if the user does not have an email address configured.
if (!$user->getEmail()) {
throw new NotFoundHttpException();
}
$message = $this->entityManager()->getStorage('contact_message')->create(array(
'contact_form' => 'personal',
'recipient' => $user->id(),
));
$form = $this->entityFormBuilder()->getForm($message);
$form['#title'] = $this->t('Contact @username', array('@username' => $user->getDisplayName()));
$form['#cache']['contexts'][] = 'user.permissions';
return $form;
}
Loading form directly from the route:
If you would like to load the form directly using the _form default, there's an example in the shortcut module at /core/modules/shortcut/shortcut.routing.yml
shortcut.set_switch:
path: '/user/{user}/shortcuts'
defaults:
_form: 'Drupal\shortcut\Form\SwitchShortcutSet'
_title: 'Shortcuts'
requirements:
_custom_access: 'Drupal\shortcut\Form\SwitchShortcutSet::checkAccess'
options:
_admin_route: TRUE
user: \d+
In this case, the user is passed in as a parameter to the form, see
/core/modules/shortcut/src/Form/SwitchShortcutSet.php
public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {