2017 © Pedro Peláez
 

cakephp-plugin cake-frontend-bridge

Frontend Bridge CakePHP 3

image

codekanzlei/cake-frontend-bridge

Frontend Bridge CakePHP 3

  • Friday, November 3, 2017
  • by cleptric
  • Repository
  • 3 Watchers
  • 2 Stars
  • 12,376 Installations
  • JavaScript
  • 9 Dependents
  • 0 Suggesters
  • 4 Forks
  • 7 Open issues
  • 59 Versions
  • 4 % Grown

The README.md

CakePHP 3 Notifications Plugin License, (*1)

A CakePHP 3 plugin that allows the creation of JavaScript Controllers for Controller-actions. Code-generation via the shell is possible as well., (*2)

Dependencies

Installation

1. require the plugin in composer.json

"require": {
    "codekanzlei/cake-frontend-bridge": "dev-master",
}

2. Include the plugin using composer

Open a terminal in your project-folder and run, (*3)

$ composer update

3. Load the plugin in config/bootstrap.php

Plugin::load('FrontendBridge', ['bootstrap' => true, 'routes' => true, 'autoload' => true]);

4. Create additional files

Create the following files and set them up using the given code., (*4)

  • app_controller.js, (*5)

    path:/webroot/js/app/, (*6)

    template code:, (*7)

    Frontend.AppController = Frontend.Controller.extend({
        baseComponents: [],
        _initialize: function() {
            this.startup();
        }
    });
    

5. Configure AppController.php

In your src/Controller/AppController.php, insert the following pieces of code to setup the Frontend Bridge plugin., (*8)

Traits:, (*9)

use \FrontendBridge\Lib\FrontendBridgeTrait;

$helpers:, (*10)

'FrontendBridge' => ['className' => 'FrontendBridge.FrontendBridge'],

$components:, (*11)

'FrontendBridge.FrontendBridge',

6. Load the scripts

Inside your <head> section add the following code to load all needed .js controllers:, (*12)

<?php if(isset($this->FrontendBridge)) {
    $this->FrontendBridge->init($frontendData);
    echo $this->FrontendBridge->run();
} ?>

In development add $this->FrontendBridge->addAllControllers(); into the if block to load without exceptions all .js controllers, (*13)

7. Add Main Content Classes

Inside your Layout\default.ctp add the following line. Inside the div should be your content., (*14)

<div <?= $this->FrontendBridge->getControllerAttributes(['some', 'optional', 'css', 'classes']) ?>>
    ...
    <?= $this->fetch('content') ?>
    ...
</div>

Also add this to other layout files, if you're using them., (*15)

Bake JS Controllers

You can bake JS Controllers for your controller actions by using the bake shell. Just pass the controller name and the camelized action name., (*16)

bin/cake bake js_controller Posts addPost

This command will create the JavaScript-controller file and if necessary the directory structure for it., (*17)

You can also bake JS controllers into your plugins by using the --plugin option., (*18)

Code Examples

Use the following example code snippets to get a better understanding of the FrontendBridge Plugin and its useful features. Note that you might need to include further Plugins for these., (*19)

1.) Access view variables in js_controller

Use the following basic setup to pass data from the Backend to the Frontend via FrontendBridge., (*20)

  • YourController.php (CakeController), (*21)

    action() {
        $this->FrontendBridge->setJson(
            'varName',
            'this is sample text');
    }
    
  • action.ctp (Cake View file), (*22)

    display content of json var
    <span class="findMe"></span>
    
  • action_controller.js (FrontendBridge JS-Controller), (*23)

    startup: function() {
        var varName = this.getVar('varName');
        $('findMe').text(varName);
    }
    

Be sure to play close attention to the naming convention, especially regarding the action_controller.js. Following the example shown above, its exact path should be /webroot/js/app/controllers/your/action_controller.js, (*24)

The data you passed from the Controller to the Frontend in the variable varName should now be rendered in the view., (*25)

2.) Use modal dialog windows for editing forms

This example uses the following additional Plugins:, (*26)

Use the following basic setup to use a modal view element as an edit form that passes the change-request data to the Backend via FrontendBridge. For this example, we will use a basic Users-Model as well as a modal form-element, embedded in the regular view page for a User-Entity to edit and save some of its attributes., (*27)

  • UsersController.php (CakeController), (*28)

    public function editModal ($id = null) {
        $user = $this->Users->get($id);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                // prevent redirecting or reloading of the current page
                $this->FrontendBridge->setJson('success', true);
            }
        }
        $this->set(compact('user'));
    }
    
  • view.ctp (regular Cake View file; edit_modal.ctp in front of it), (*29)

    // CkTools button
    <?=$this->CkTools->button('edit_label', [
        'controller' => 'Users',
        'action' => 'editModal',
        $user->id
        ],
        // block interaction with the view file 'behind' the modal element
        ['additionalClasses' => 'modal-form']
    )?>
    
  • edit_modal.ctp (View file which will be the content of the modal form), (*30)



  • edit_modal_controller.js (FrontendBridge JS-Controller), (*31)

    startup: function() {
        //  add an EventListener to element with class 'modal-form'
        this.$('.modal-form').on('click', function(e) {
            // callback function when Listener is triggered
            e.preventDefault();
            App.Main.Dialog.loadDialog($(e.currentTarget).attr('href'), {
                modalTitle: 'Modal title',
                preventHistory: false
            });
        });
    }
    

3.) Generate custom AJAX requests

Use the following setup to generate custom AJAX requests. Using FrontendBridge's 'request' action, we will pass JSON data from the Backend to the Frontend., (*32)

  • YourController.php (CakeController), (*33)

    public function getJsonData()
    {
        $code = ApiReturnCode::SUCCESS;
        return $this->Api->response($code, [
            'foo' => 'bar',
            'more' => 'json-data'
        ]); // JSON data which will be logged in your browser console
    }
    
  • index.ctp (Cake View file), (*34)

    <a class="ajax-json-demo">Get JSON data</a>
    
  • index_controller.js (FrontendBridge JS-Controller), (*35)

    App.Controllers.YourIndexController = Frontend.AppController.extend({
        startup: function() {
            this.$('.ajax-json-demo').click(function() {
                var url = {
                    controller: 'Your',
                    action: 'getJsonData'
                };
                // if postData is null, a GET request will be made
                var postData = null;
                App.Main.request(url, postData, function (response) {
                    alert('Response Code ' + response.code + ' - see console for details');
                    console.log(response.data);
                });
            }.bind(this));
        }
    });
    

4.) Load content dynamically using AJAX

Use the following setup to make custom HTML requests via AJAX. Using FrontendBridge's loadJsonAction and request, we will implement a very basic livesearch., (*36)

This example assumes a Table called Users with a field username in your database., (*37)

  • SearchController.php (CakeController), (*38)

    public function index() {
        // renders index.ctp
    }
    
    public function search() {
        $users = null;
        if ($this->request->is(['patch', 'post', 'put']) && !empty($this->request->data['search'])) {
            // search UsersTable for entities that contain
            // request->data['search'] in field 'username'
            $users = TableRegistry::get('Users')->find()
                ->where(['username LIKE' => '%' . $this->request->data['search'] . '%'])
                ->all();
        }
        $this->set(compact('users'));
    }
    
  • index.ctp (Cake View file), (*39)

    <!-- input field -->
    <div class="input-group">
        <input type="text" class="form-control" id="search">
    </div>
    <br />
    <!-- search results (search.ctp) will be rendered here -->
    <div class="results"></div>
    
  • search.ctp (Cake View file), (*40)

    <?php if(!empty($users)) : ?>
        <?php foreach($users as $user) : ?>
            <?= h($user->username) ?><br />
        <?php endforeach; ?>
    <?php else : ?>
        no results to display
    <?php endif; ?>
    
  • index_controller.js (FrontendBridge JS-Controller), (*41)

    App.Controllers.SearchIndexController = Frontend.AppController.extend({
        startup: function() {
            // set KeyUp-EventListener to field with id 'search' in index.ctp
            this.$('#search').keyup(this._search.bind(this));
        },
    
        // called at eatch keyup-event in 'search'
        _search: function() {
            var $renderTarget = this.$('.results');
            var url = {
                controller: 'Search',
                action: 'search'
            }
    
            // create a custom AJAX request with the user input included in the post-data
            App.Main.loadJsonAction(url, {
                data: {
                    search: this.$('#search').val()
                },
                target: $renderTarget, // render the HTML into this selector
            });
        }
    });
    

Functionality Overview

  • loadJsonAction, (*42)

    Allows dynamically loading content in the view using AJAX according to user interaction. Uses request., (*43)

  • request, (*44)

    Allows generating custom AJAX requests. Also supports FormData (except IE <= 9) as request data., (*45)

The Versions

03/11 2017

dev-2.next

dev-2.next

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

03/11 2017

v2.0.0-rc3

2.0.0.0-RC3

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

03/11 2017

v2.0.0-rc2

2.0.0.0-RC2

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

18/08 2017

dev-master

9999999-dev

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

18/08 2017

v2.0.0-rc1

2.0.0.0-RC1

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

07/08 2017

v1.4.14

1.4.14.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

07/08 2017

v1.4.13

1.4.13.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

03/08 2017

v1.4.12

1.4.12.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

17/07 2017

v1.4.11

1.4.11.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

17/07 2017

dev-pass-config-to-submit

dev-pass-config-to-submit

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

13/07 2017

v1.4.10

1.4.10.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

13/07 2017

v1.4.9

1.4.9.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

13/07 2017

v1.4.8

1.4.8.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

13/07 2017

v1.4.7

1.4.7.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

11/07 2017

v1.4.6

1.4.6.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

11/07 2017

v1.4.5

1.4.5.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

11/07 2017

v1.4.4

1.4.4.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

10/07 2017

v1.4.3

1.4.3.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

10/07 2017

v1.4.2

1.4.2.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

10/07 2017

v1.4.1

1.4.1.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

04/07 2017

v1.4.0

1.4.0.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

04/07 2017

v1.3.5

1.3.5.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

04/07 2017

v1.3.4

1.3.4.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/06 2017

v1.3.3

1.3.3.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/06 2017

v1.3.2

1.3.2.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/06 2017

v1.3.1

1.3.1.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/06 2017

v1.3.0

1.3.0.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

07/04 2017

v1.2.12

1.2.12.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

07/04 2017

dev-dev

dev-dev

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

06/04 2017

v1.2.11

1.2.11.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

05/04 2017

v1.2.10

1.2.10.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

05/04 2017

v1.2.9-beta

1.2.9.0-beta

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

05/04 2017

v1.2.9

1.2.9.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

03/04 2017

v1.2.8

1.2.8.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

31/03 2017

v1.2.7

1.2.7.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/03 2017

v1.2.6

1.2.6.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/03 2017

v1.2.5

1.2.5.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/03 2017

v1.2.4

1.2.4.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/03 2017

v1.2.3

1.2.3.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/03 2017

v1.2.2

1.2.2.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

28/03 2017

v1.2.1

1.2.1.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

27/03 2017

v1.2.0

1.2.0.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

23/03 2017

v1.1.1

1.1.1.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

16/03 2017

v1.1.0

1.1.0.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

15/03 2017

v1.0.12

1.0.12.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

30/01 2017

v1.0.11

1.0.11.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

10/01 2017

v1.0.10

1.0.10.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2017

v1.0.9

1.0.9.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

13/12 2016

v1.0.8

1.0.8.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

11/11 2016

v1.0.7

1.0.7.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

11/11 2016

dev-improve-error-handling

dev-improve-error-handling

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

21/10 2016

v1.0.6

1.0.6.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

23/09 2016

v1.0.5

1.0.5.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

21/03 2016

v1.0.4

1.0.4.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

14/09 2015

v1.0.3

1.0.3.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

03/05 2015

v1.0.2

1.0.2.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

20/04 2015

v1.0.1

1.0.1.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

09/02 2015

v1.0.0

1.0.0.0

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires

12/01 2015

dev-cake3-rc-update

dev-cake3-rc-update

Frontend Bridge CakePHP 3

  Sources   Download

MIT

The Requires

 

The Development Requires