dev-master
9999999-devRequire.js plugin for CakePHP
Apache-2.0
The Requires
- php >=5.3.0
- composer/installers *
Require.js plugin for CakePHP
This module facilitate the use of Requirejs through the Require helper., (*1)
Copy this plugin in a folder named app/Plugin/GtwRequire
or add these lines to your composer.json
file :, (*2)
{ "require": { "phillaf/gtw_require": "*@dev" } }
Create a symlink from this plugin's webroot to the application webroot by running Console/cake symlink
or the lines below, (*3)
# On windows mklink /J app\webroot\GtwRequire app\Plugin\GtwRequire\webroot # On linux ln -s app/Plugin/GtwRequire/webroot app/webroot/GtwRequire
Load the plugin by adding this line to app/Config/bootstrap.php
, (*4)
CakePlugin::load('GtwRequire');
Add the Require helper to your AppController.php, (*5)
public $helpers = array( 'Require' => array('className' => 'GtwRequire.GtwRequire'), );
Create a config file as specified by requirejs. You can find a more exhaustive example here., (*6)
// app/webroot/js/config.js requirejs.config({ baseUrl: 'js/lib', paths: { app: '../app', basepath: '/GtwRequire/js/basepath' //application base path } });
Load modules from your views, view blocks and elements using the req function., (*7)
<?php echo $this->Require->req('my/module'); ?>
Load the main requirejs module at the bottom of your layout file. The param is the path of your config file., (*8)
<?php echo $this->Require->load('/js/config');?>
The req()
function can only be called before load()
. However, in some case like with ajax views, you'll need to add modules dynamically. For this you can use:, (*9)
<?php echo $this->Require->ajax_req('my/module');?>
If you want to auto-load action-specific or controller-specific files, you can create a folder structure like the one used in the Views., (*10)
โโโ lib โ โโโ library1.js โ โโโ library2.js โโโ app โ โโโ homemade1.js โ โโโ homemade2.js โโโ base โ โโโ Example /* folder containing ExampleController.php actions */ โ โ โโโ someaction.js โ โ โโโ otheraction.js โ โโโ base.js โ โโโ ExampleController.js โ โโโ AnotherController.js โโโ require.js
To enable auto-load, call the following function above your load()
call. First param is the base path of your folder structure, second param is the name of the default file., (*11)
<?php echo $this->Require->basemodule('basePath', 'baseModuleName');?>
Sometimes you need to call controllers and actions from your javascript. In order to keep your .js module functionnal when websites are hosted on subdomains, use basepath
to prefix your calls. Here's how modules can make use of this:--, (*12)
require(['jquery','basepath'], function ($, basepath) { $('.something').click(function(){ $.get( basepath+ "plugin_name/controller_name/action_name/", function(data){ // do things }); }); });
Author: Philippe Lafrance
Copyright 2013 Gintonic Web
Licensed under the Apache 2.0 license, (*13)
Require.js plugin for CakePHP
Apache-2.0