Answers.Body: <p>Alright! I've been able to fix my problem. I did a couple of things wrong so I'm writing this for whoever makes the same mistakes I made and is now trapped in an endless loop of despair and impending doom.</p>
  <p><strong>First mistake!</strong></p>
  <p>You should leave the action attribute of your form element empty.
  Call you action by using a hidden input, like this</p>
  <pre><code>&lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;/import-products/product/create&quot;&gt;
  </code></pre>
  <p>This will automatically keep the user on your page, so there's no need to redirect in the controller and break the CP while doing it.</p>
  <p><strong>Second mistake!</strong></p>
  <p>I shouldn't have used $this-&gt;renderTemplate() at the end of my controller. It's not the preferred flow of doing it. After your controller did everything it needed to do, you should either stay on the page and display feedback or redirect to another page.</p>
  <p>Staying on the page to display feedback can be done by sending variables back to the template. That's done like so:</p>
  <pre><code>Craft::$app-&gt;getUrlManager()-&gt;setRouteParams([
          'variable' =&gt; $myVariable
  ]);
  
  return null;
  </code></pre>
  <p>If you want to redirect to a confirm-page. You should use the redirectToPostedUrl.
  To be able to use this, you need a hidden input field with a url as value. Luckily Craft has a Twig function to do so.</p>
  <pre><code>{{ redirectInput(url|hash) }}
  </code></pre>
  <p><strong>The URL needs to be hashed</strong> otherwise craft will throw an Invalid Body Parameter error. In your controller you can end your action with:</p>
  <pre><code>//You should wrap this in an if that checks if there are no errors
  return $this-&gt;redirectToPostedUrl($variable);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-13T07:27:58.527Z
Answers.LastActivityDate: 2020-11-13T07:27:58.527Z
Answers.UserDisplayName: Kap1tein
Body: <p>I'm locally working on a Craft Plugin with a Page in the Control Panel where I have a form that posts to a controller.</p>
  <pre><code>&lt;form action=&quot;/actions/import-products/product/create&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
  ...
  &lt;/form&gt;
  </code></pre>
  <p>The controller returns the following at the of it's action</p>
  <pre><code>// Render and return the plugin's 'index.twig' template
  return $this-&gt;renderTemplate('import-products/index.twig', $variables, View::TEMPLATE_MODE_CP);
  </code></pre>
  <p>However, after the renderTemplate, the CP navigation is broken.
  It seems that Craft is using the base URL to build the navigation items. Where it should be localhost/admin/dashboard, it's localhost/dashboard? How do I fix this?</p>
  <p>Changing the Base URL of the site to localhost/admin breaks everything because the default routes are changed to localhost/admin/admin/dashboard.</p>
  
Tags: <plugin-development><control-panel><controller>
Title: $this->renderTemplate() breaks the CP navigation
ThreadText: # $this->renderTemplate() breaks the CP navigation
  
  (Asked by: username_0 on 2020-11-10)
  
  I'm locally working on a Craft Plugin with a Page in the Control Panel where I have a form that posts to a controller.
  
  ```
  <form action="/actions/import-products/product/create" method="POST" enctype="multipart/form-data">
  ...
  </form>
  
  ```
  
  The controller returns the following at the of it's action
  
  ```
  // Render and return the plugin's 'index.twig' template
  return $this->renderTemplate('import-products/index.twig', $variables, View::TEMPLATE_MODE_CP);
  
  ```
  
  However, after the renderTemplate, the CP navigation is broken.
  It seems that Craft is using the base URL to build the navigation items. Where it should be localhost/admin/dashboard, it's localhost/dashboard? How do I fix this?
  
  Changing the Base URL of the site to localhost/admin breaks everything because the default routes are changed to localhost/admin/admin/dashboard.
  
  **Tags:** plugin-development, control-panel, controller
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-11-13. Score: 1 | Accepted answer
  
  Alright! I've been able to fix my problem. I did a couple of things wrong so I'm writing this for whoever makes the same mistakes I made and is now trapped in an endless loop of despair and impending doom.
  
  **First mistake!**
  
  You should leave the action attribute of your form element empty.
  Call you action by using a hidden input, like this
  
  ```
  <input type="hidden" name="action" value="/import-products/product/create">
  
  ```
  
  This will automatically keep the user on your page, so there's no need to redirect in the controller and break the CP while doing it.
  
  **Second mistake!**
  
  I shouldn't have used $this->renderTemplate() at the end of my controller. It's not the preferred flow of doing it. After your controller did everything it needed to do, you should either stay on the page and display feedback or redirect to another page.
  
  Staying on the page to display feedback can be done by sending variables back to the template. That's done like so:
  
  ```
  Craft::$app->getUrlManager()->setRouteParams([
          'variable' => $myVariable
  ]);
  
  return null;
  
  ```
  
  If you want to redirect to a confirm-page. You should use the redirectToPostedUrl.
  To be able to use this, you need a hidden input field with a url as value. Luckily Craft has a Twig function to do so.
  
  ```
  {{ redirectInput(url|hash) }}
  
  ```
  
  **The URL needs to be hashed** otherwise craft will throw an Invalid Body Parameter error. In your controller you can end your action with:
  
  ```
  //You should wrap this in an if that checks if there are no errors
  return $this->redirectToPostedUrl($variable);
  
  ```
  
  
  ---
  
  