WintersWolf WintersWolf - 1 year ago 119
Javascript Question

Error redirects for OpenCart with UserFrosting

So I have been working on integrating OpenCart in to UserFrosting ( Basically I am making all the admin dashboard pages load in the UserFrosting dashboard by using

to inject the pages into a
on my own html pages. I am almost finished but have one last piece to get working that isn't remaining within the UF dashboard.

I will use the Catalog / Categories page as an example to show what I am doing:

Here is my html file for categories, it's called cata-categories.html

<!DOCTYPE html>
<html lang="en">
{% include 'components/head.html' %}
<script src=""></script>
<div id="wrapper">
{% include 'components/nav-account.html' %}
<div id="page-wrapper">
{% include 'components/alerts.html' %}

<ol id="categories"></ol>

$( "#categories" ).load( "../solutions/admin/index.php?route=catalog/category" );

{% include 'components/footer.html' %}

This is accessed via the sidebar of UserFrosting with the following code in sidebar.html:

{% if checkAccess('uri_manage_groups') %}
<a href="{{site.uri.public}}/cata-categories/"><i class="fa fa-tags fa-fw"></i>Categories</a>
{% endif %}

The page is rendered via the following code in public/index.php for UserFrosting:

$app->get('/cata-categories/?', function () use ($app) {
$app->render('cata-categories.html', [
'page' => [
'author' => $app->site->author,
'title' => "Catagories",
'description' => "Catalogue Catagories.",
'alerts' => $app->alerts->getAndClearMessages()

To keep all links clicked on the OpenCart pages I put the following script on the bottom of each main
(so for example

$("#categories").on("click", "a", function (e) {

Then to keep things on the same page when submitting forms I changed the redirects in the controllers (eg. category.php) to be the following:


Everything works great, now the only issue is errors in the form submission or filtering results. For example if someone tries to add a new category but doesn't fill in any information and clicks save, it redirects to
but not within the UserFrosting dashboard.

I want it to remain within my dashboard, and need a nudge in the right direction on what to modify within the controller to get it to do this, I'm assuming some sort of AJAX to force it not to reload the page and just show the errors. Same thing with filter buttons, it obviously needs to add filters to the URL and therefore doesn't load within the dashboard - need a way to filter without refreshing.

Any help from people with experience modifying OpenCart would be greatly appreciated and if you need any more info on what I am doing feel free to ask.

Answer Source

OpenCart's routes are not built for AJAX requests

The way that UserFrosting and OpenCart handle POST requests seem to be fundamentally at odds. If you look at the code for the catalog/category/add route (or at least, that's what I think it is, but the code doesn't seem to have any in-code documentation so it's hard to be sure), you'll see this:

public function add() {



    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {

        $this->session->data['success'] = $this->language->get('text_success');

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];

        $this->response->redirect($this->url->link('catalog/category', 'token=' . $this->session->data['token'] . $url, 'SSL'));


So what happens here? It would seem that when you POST to catalog/category/add, it starts to generate a document (I'm guessing that is what the first three lines are doing), and then it processes your POST request (I believe $this->model_catalog_category->addCategory($this->request->post); actually adds the category).

Then, it rebuilds the URL for listing all of the categories (I assume), and actually redirects the response to that page (still in the server-side code that processes the POST request, mind you). This is all fine, and is actually how most old-school (pre-AJAX) applications worked, but UF does things a little differently.

UserFrosting's routes are (mostly) RESTful

In UF, a POST request is RESTful, which means that the only task of a POST request is to modify a resource. So when you click "submit" in a UserFrosting form, the POST request is dispatched behind the scenes, by AJAX, where it is processed (either successfully updating the resource and returning an HTTP 200 success code, or failing for some reason and returning an error code). If the page needs to be refreshed or redirected after this request, it is the responsibility of the client-side (Javascript) code to do this.


My advice would be to discard OpenCart's controller entirely, and re-implement it in a RESTful pattern. So instead of having OpenCart's add() method, you should have methods like:

  1. formCategoryAdd: generates the form to add a category. This might be as simple as a wrapper around OpenCart's getForm method. This is basically the same type of thing that UserFrosting's formUserCreate does.
  2. addCategory: this actually processes the POST request to add a category. Again, you might need to do nothing more here than call OpenCart's addCategory and validateForm methods. The only difference is that you won't redirect the request when it's finished. Instead, you'll add a success message to the message stream (if it succeeds in adding the category), or you'll add an error message and then return an appropriate error code (if it fails for some reason). See UserFrosting's createUser method for an example of how this could work.

Then, back on the client side, you'll need some Javascript to process the form submission. You can basically steal it from any of the UF pages. Assuming your form is named add-category:

    $(document).ready(function() {            
      // Process form 
        framework: 'bootstrap',
        // Feedback icons
        icon: {
            valid: 'fa fa-check',
            invalid: 'fa fa-times',
            validating: 'fa fa-refresh'
        fields: { "" : ""}
      }).on('success.form.fv', function(e) {
        // Prevent double form submission
        // Get the form instance
        var form = $(;
        // Serialize and post to the backend script in ajax mode
        var serializedData = form.serialize();            
        var url = form.attr('action');
          type: "POST",  
          url: url,  
          data: serializedData       
        }).done(function(data, statusText, jqXHR) {
            // Forward to account home page on success
        }).fail(function(jqXHR) {
            if ((typeof site !== "undefined") && site['debug'] == true) {
                document.body.innerHTML = jqXHR.responseText;
            } else {
                console.log("Error (" + jqXHR.status + "): " + jqXHR.responseText );
                // Display errors on failure
                $('#userfrosting-alerts').flashAlerts().done(function() {
                    // Re-enable submit button

If you want to reduce client-side code repetition, I've put this Javascript into a separate function in the dev branch:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download