Using bootstrap.php To Configuration Folders Containing Source Code in Cakephp

As we know, cakephp written in MVC structure, so when we write codes of a program, we almost write code in app folder as: model,  controller, views, component(in controller) and helper(in views). Almost of files source have been written in them.



Today, i want to guide you how to transfer source files to a folder that other app folder. The transfer code from app folders to outside have some advantage:
– Manage code files will easier, clearer and easy to maintain. When we transfer our files to a a other folder, search will become easier than search a file in a lot of files in app.
– With user who used svn, this will create a big advantage. Instead of we must send all code (include unnecessary files), we only send a folder what include necessary file.
Updating cakephp new version will become easier because our program is out of cakephp folders. Instead of before updating new version of cakephp, we must copy each folder, we only override the old folders. Our files are not affected at all.

There are a lot of advantage that i haven’t mentioned. But i think it’s enough to your try. The following, i will transfer data in : controllers, views and models folders (absolutly include all sub folders in them as : helpers, components…). And we will transfer them to bitjsc folder that is same level with app folder.

Here are the things to do:
– First, we will create some necessary folders include:
+ bitjsc folder (same level with app folder and cake folder).
+ Sub folders of bitjsc include : controllers, views, models and config.
+ Sub folders of controller, views, models and config as : helpers, component …
– After that we will configure bootstrap.php file with path /app/config/bootstrap.php . We add to end of file this line: 

require_once(dirname(__FILE__) . DS . '..' . DS . '..' . DS . 'bitjsc' . DS . 'config' . DS . 'bootstrap.php');

The line above will call bootstrap.php file from bitjsc/config/ , this is folder that is created in above. In bitjsc/config/ ,  we will create a file with name is bootstrap.php. In this file, we will execute the trap to when we call models, controllers or views, program will auto call corresponding file in bitjsc folder instead of call them in app folder.
– Create bootstrap.php file with path  /bitjsc/config/bootstrap.php, content of this file:

<?php
/**
* Bitjsc bootstrap file
*
* This file should be included in app/bootsrap.php. It connect WF
* with your application.
*
* @package bitjsc
*/
// Bitjsc MVC paths
define('BITJSC_DIR', ROOT . DS . 'bitjsc');
define('SETTINGS_CACHE_FILE', TMP . 'settings' . DS . 'cache');
$modelPaths = array(BITJSC_DIR . DS . 'models' . DS);
$viewPaths = array(BITJSC_DIR . DS . 'views' . DS);
$controllerPaths = array(BITJSC_DIR . DS . 'controllers' . DS);
$behaviorPaths = array(BITJSC_DIR . DS . 'models' . DS . 'behaviors' . DS);
$helperPaths = array(BITJSC_DIR . DS . 'views' . DS . 'helpers' . DS);
$componentPaths = array(BITJSC_DIR . DS . 'controllers' . DS . 'components' . DS);
?>
 

So, we completed configuring path. Now, when we create any new file controller, views or models, you only create them in corresponding folder in bitjsc. Good luck

Leave a Reply

Your email address will not be published. Required fields are marked *