Utility Open Source Apps In CakePHP That You Need To Know

CakePHP is one of the most popular open source web application framework. It provides an extensible architecture for development, maintenance and deployment of PHP applications. Being based on the MVC Architecture & ORM, it is relatively simple to understand and helps developers to write less code without losing flexibility.

The key features of Open Source Apps written in CakePHP are:

  • Flexible Licensing
  • Works from any Website Directory, with Little Number of Apache Configuration involved
  • Integrated CRUD for Database Interaction
  • Code Generation & Built-in Validation
  • Request Dispatcher with Clean, Custom URLs and Routes
  • Fast and Flexible Templating (PHP Syntax with Helpers)
  • View Helpers for AJAX, JavaScript, HTML Forms and much more
  • Email, Cookie, Security, Session and Request Handling Components
  • Flexible ACL and Data Sanitization
  • Flexible Caching and Localization

CakePHP has the second largest active developer team and community among the PHP frameworks, bringing great value to the project. It keeps you away from reinventing the wheel.

Never miss an update from us. Join 10,000+ marketers and leaders.

Github is the #1 resource for Cakephp projects in my opinion. 2nd place probably is occupied by Code Google and then comes Sourceforge.

Orangescrum

Orangescrum is a flexible project management web application developed using CakePHP framework. It’s an efficient Project Collaboration Tool that gives you full visibility and control over your projects. Orangescrum offers all the basic features you need for smooth running of your project. No matter where you are globally, with Orangescrum you feel like sitting next to your developer. With an intuitive interfaces, easy-to-use functionality, you organize your project activities to a great extent.

Disclaimer: It’s our own project management tool.

https://github.com/Orangescrum/orangescrum

Croogo

Croogo is a free, open source, Multilingual Content management system built using CakePHP MVC framework. It’s an open source project released under MIT License. With Croogo CMS, you can create your own content type such as blog, node, page, etc.  Also you can categorize content with Taxonomy and it also offers WYSIWYG editor to edit content directly from browser.

Zhen CRM

Zhen CRM is a self-hosted CRM with full source code (in CakePHP and MySQL) that aims to be visually easy to use, simple and straightforward, and provides all the features you need from a full-featured CRM!

Vamcart

Vamcart is a CakePHP based open source Shopping cart. It is easy to use and simple to install. It is not required to have any special knowledge to install Vamcart on your server. Just download the files from vamcart homepage and put them on your server’s root directory and open Vamcart installation page directly from the browser. The script will automatically install shopping cart on your site.

QuickApps

QuickApps is a CakePHP based open source Content management system. It is easy to use and simple to install. It allows developers and advanced users to easily build complex websites without reinventing the wheel.

 

Recommended Blog: Containable Behavior in CakePHP

Definitely, this list is incomplete. Share your favorite open source CakePHP application with us in the comments below.

How To Do Custom Pagination In CakePHP

Sometimes we need to do pagination in CakePHP using a traditional SQL query, thereby the CakePHP predefined pagination functionality is lost.

We can implement pagination method in the Model or Behavior when we are using standard SQL query in CakePHP. Just need to make sure that the result cannot be obtained with core model methods, or a custom finder before implementing custom queries pagination.

But we can’t just use the standard pagination on the custom queries, we have to override the paginate functions in model or behavior.

To use our own method/logic to override the CakePHP pagination in the model, we need to add two functions: paginate() and paginateCount().

In these functions, we can implement our own paginate logic.

Overriding paginate() function in CakePHP Post Model:

[php]public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$orderStr = ”;
foreach($order as $k => $ord) {
$orderStr[] = $k . ‘ ‘ . $ord;
}
$orderStr = ‘ORDER BY ‘. implode(‘, ‘, $orderStr);

$qryCond = ‘1’;
if (isset($conditions[‘Post.title LIKE’])) {
$qryCond = ‘title LIKE \”.$conditions[‘Post.title LIKE’].’\”;
}

$qryFlds = ‘*’;
if ($fields) {
$qryFlds = implode(‘, ‘, $fields);
}

$sql = ‘SELECT ‘.$qryFlds.’ FROM posts as Post WHERE ‘.$qryCond.’ ‘.$orderStr . ‘ LIMIT ‘ . (($page-1) * $limit) . ‘, ‘ . $limit;
$results = $this->query($sql);
return $results;
}[/php]

Overriding paginateCount() function in CakePHP Post Model:

[php]public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$qryCond = ‘1’;
if (isset($conditions[‘Post.title LIKE’])) {
$qryCond = ‘title LIKE \”.$conditions[‘Post.title LIKE’].’\”;
}

$sql = ‘SELECT COUNT(*) as count FROM posts as Post WHERE ‘.$qryCond;

$this->recursive = -1;

$results = $this->query($sql);
return $results[0][0][‘count’];
}[/php]

Using in the Post Controller: We can adjust the fields to get, column ordering, add certain conditions or adjust row limit per page.

[php]public function index() {
$this->paginate = array(
‘limit’ => 10,
‘fields’ => array(‘Post.id’, ‘Post.title’, ‘Post.created’),
‘conditions’ => array(‘Post.title LIKE’ => ‘%search_keyword%’),
‘order’ => array(‘Post.title’ => ‘asc’, ‘Post.id’ => ‘asc’)
);
try {
$this->set(‘posts’, $this->paginate(‘Post’));
} catch (NotFoundException $e) {
$this->redirect(‘index_custom’);
}
}[/php]

The above example is based on specific model and requirement, need to adjust the methods according to the requirements.

See Also: Creating a custom handler session in CakePHP 2.x

Best of luck and happy pagination.

Hope you liked this. Let me know if you want to add anything?

How To Setup CakePHP DataSource For Solr?

Imagine that you have millions of data and your SQL database is not optimized enough to handle them, then “Solr” is your best data source. Apache Solr is a fast search platform. It’s major features include full-text search faceted search, dynamic clustering, database integration, rich document handling. Solr is highly scalable and it is the most popular enterprise search engine.

This document will guide you through DataSource setup for Solr in CakePHP. Set-up the Solr DataSource in CakePHP and query Solr with your CakePHP model “find” function, just similar to querying any other SQL database.

/app/Config/database.php:

Configuring your the DataSource for Solr database connection:

  • host: Solr server address.
  • port: Solr server port.
  • datasource: This should match with your DataSource at /app/Model/Datasource/SolrSource.php

[php]class DATABASE_CONFIG {
public $solr = array(
‘datasource’ => ‘SolrSource’,
‘host’ => ‘192.168.2.131’,
‘port’ => ‘9983’,
‘path’ => ‘/solr/’
);
}[/php]

/app/Model/Solr.php:

Use the database config in your models like:

[php]class Solr extends AppModel {
 public $useTable = false;
 public $useDbConfig = ‘solr’;
 }[/php]

/app/Model/Datasource/SolrSource.php:

Below code describes only the most required functions.

  • You can find other required functions at http://book.cakephp.org/2.0/en/models/datasources.html
    Like: calculate($model, $func, $params) and others create(), update(), delete().
  • Below DataSource only implements read() function.
  • Create a file called SolrSource.php and use below code:

[php]App::uses(‘HttpSocket’, ‘Network/Http’);
require_once($_SERVER[‘DOCUMENT_ROOT’].’/root_folder/app/Vendor/Apache/Solr/Service.php’);
class SolrSource extends DataSource {

protected $_schema = array();
protected $_host;
protected $_port;

public function __construct($config){
parent::__construct($config);
$this->Http = new HttpSocket();

$this->host = $config[‘host’];
$this->port = $config[‘port’];
$this->_schema = $config[‘path’];

$this->solr=new Apache_Solr_Service($this->host, $this->port, $this->_schema);
}

public function read(Model $model, $queryData = array(), $recursive = null) {
$results = array();
$query = $queryData[‘conditions’][‘solr_query’];
if (Configure::read(‘log_solr_queries’) === true) {
CakeLog::write(‘debug’, $query); /* Logs the solr query in app/tmp/logs/debug.log file */
}

try {
$solr_docs = $this->solr->search($query, $queryData[‘offset’], $queryData[‘limit’], array(‘sort’ => $queryData[‘order’]));
} catch (Exception $e) {
CakeLog::write(‘error’, $e->getMessage()); /* Logs the solr errors message in app/tmp/logs/error.log file */
CakeLog::write(‘error’, $query); /* Logs the solr query in app/tmp/logs/error.log file */
}
$model->params = $solr_docs->responseHeader->params;
$model->numFound = $solr_docs->response->numFound;
$docs = $solr_docs->response->docs;
foreach ($docs as $doc) {
$document = array();
foreach ($doc as $fieldName => $fieldValue) {
$document[$fieldName] = $fieldValue;
}
$results[] = $document;
}
return array($model->alias => $results);
}
}[/php]

/app/Controller/UsersController.php:

Getting data from Solr server by using above SolrSource.

[php]App::uses(‘AppController’, ‘Controller’);
class UsersController extends AppController {

public $name = ‘Users’; //Controller name
public $uses = array(‘Solr’); //Model name

function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow(‘index’);
}

public function index(){
$cond = "active:1";
$sort = "timestamp desc";

$params = array(
‘conditions’ =>array(
‘solr_query’ => $cond
),
‘order’ => $sort,
‘offset’ => 0,
‘limit’ => 1
);

$result = $this->Solr->find(‘all’, $params);
$this->layout = ‘ajax’;
header(‘Content-Type: application/json; charset=utf-8’); /* To send response in json format */
echo json_encode($result);
die;
}
}[/php]

See Also : How to use ‘neighbors’ with ‘find’ method

Debugging:

The above code has been tested and validated with CakePHP 2.2.3, PHP 5.3.1 .
Incase you face any problems:

  • First check the DataSource configuration in /app/Config/database.php.
  • Check /app/Model/Solr.php for this line: public $useDbConfig = ‘solr’;
  • Make sure Solr Service Vendor loaded in /app/Model/Datasource/SolrSource.php.
  • Check the path is correct for the Solr vendor.

Hope you liked this. Let me know if you want to add anything?

Hash Class: Makes CakePHP Coding easier!

Hash is a predefined class provided by CakePHP. It is used for array manipulation such as inserting an element to an array, remove an element from an array, sort an array, extract part of a large array, filter the non empty elements, rearrange the whole array, which makes the code more optimized and understandable. So it makes CakePHP easier and flexible to use. Because most of the operations like find, insert, update in CakePHP returns/takes array as output/input.

Hash provides an improved interface, more consistent and predictable set of features over Set. While it lacks the spotty support for pseudo XPath, its more fully featured dot notation provides similar features in a more consistent implementation.

Operations performed by Hash class:

  • extract()
  • combine()
  • filter()
  • check()
  • insert()
  • remove()
  • sort()
  • and many more…

Some Important Tips:

{n} – Match any numeric key.
{s} – Match any string value and numeric string values.
[id] – Matches elements with a given array key.
[id=2] – Matches elements with id equal to 2.
[id!=2] – Matches elements with id not equal to 2.
[id<=2] – Matches elements with id less than or equal to 2.

– Matches elements that have values matching the regular expression inside.

  • Hash::extract(array $data, $path):

Retrieve required data from array. You do not have to loop through the array.

Ex: // Common Usage:

$users = $this->User->find("all");
 $results = Hash::extract($user, '{n}.User.id');
 // $results equals:
 // array(1,2,3,4,5,...);
  • Hash::insert(array $data, $path, $values = null):

Insert sub-array’s into the original array.

Ex:

$temp = array(
 'page' => array('name' => 'page')
);
$result = Hash::insert($temp, 'file', array('name' => 'files'));
// $result now looks like:
Array
(
 [page] => Array
 (
 [name] => page
 )
 [file] => Array
 (
 [name] => files
 )
)
  • Hash::remove(array $data,  $path = null):
Remove data from any path specified.

EX:

$arr_test = array(
 'page' => array('name' => 'page'),
 'file' => array('name' => 'files')
);
$result = Hash::remove($arr_test, 'file');
/* $result now looks like:
 Array
 (
 [page] => Array
 (
 [name] => page
 )
 
)
*/
  • Hash::combine(array $data$keyPath = null$valuePath = null$groupPath = null)

Combine the results to form a new array of expected result. Helpful in case, where we are displaying data in the form of select box. Like categories, states, city etc. We don’t have to retrieve the data separately. We can find the required data from the original result set retrieved, containing the information..

Ex:

$arr_test = array(
 array(
 'User' => array(
 'id' => 2,
 'group_id' => 1,
 'Data' => array(
 'user' => 'John.doe',
 'name' => 'Matt Lee'
 )
 )
 ),
 array(
 'User' => array(
 'id' => 14,
 'group_id' => 2,
 'Data' => array(
 'user' => 'phpunt',
 'name' => 'Jack'
 )
 )
 ),
);
 
$result = Hash::combine($arr_test, '{n}.User.id', '{n}.User.Data');
/* $result now looks like below:
 Array
 (
 [2] => Array
 (
 [user] => John.doe
 [name] => Matt Lee
 )
 [14] => Array
 (
 [user] => phpunt
 [name] => Jack
 )
 )
*/
  • Hash::check(array $datastring $path = null)
    Check whether an element exists in the array or not.

Ex:

$set = array(
 'My Index' => array('First' => 'The first item1')
);
$result = Hash::check($set, 'My Index.First');
// $result == True
  • Hash::filter(array $data$callback = array(‘Hash ‘‘Filter’)):
Keep only non-empty elements and filter the empty elements.
Ex:
$data_arr = array(
 '0',
 false,
 true,
 0,
 array('test', 'is you own', false)
);
$result = Hash::filter($data_arr);
 
/* Out put:
 Array (
 [0] => 0
 [2] => true
 [3] => 0
 [4] => Array
 (
 [0] => one thing
 [2] => is you own
 )
 )
*/
  • Hash::sort(array $data$path$dir$type = ‘regular’)

Sort an array according to the path, direction and type provided.

Ex:

$arr_test = array(
 0 => array('Person' => array('name' => 'Jeff1')),
 1 => array('Shirt' => array('color' => 'black1'))
);
$result = Hash::sort($arr_test, '{n}.Person.name', 'asc');
/* $result now looks like:
 Array
 (
 [0] => Array
 (
 [Shirt] => Array
 (
 [color] => black1
 )
 )
 [1] => Array
 (
 [Person] => Array
 (
 [name] => Jeff1
 )
 )
 )
*/

$type can be of the following type:

regular
numeric
string
natural(ex. Will sort fooo10 below fooo2 as an example)

$dirc can be of two type asc & desc

For more information refer: http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html

However if you want you can hire or get free consultation from our experienced CakePHP developers.

Read More: Password Hashing API in PHP

Have I missed out anything? Comment at end of this topic.

Using AJAX, How to Make Your Site Crawlable

AJAX_Crawler-image

Why Ajax?

AJAX is used to load the contents into the page faster with providing a superior user experience. It happens so as the data exchanges with a server and the web page reflects the changed data without reloading. So AJAX is gaining popularity among the developers.

Problems with AJAX usage:

However, it has its own limitation for not allowing Google and other search engines to crawl an AJAX based website. As a result of which AJAX can damage the SEO of the site, because the browser address bar does not change with AJAX load. You need to generate SEO friendly URL dynamically.

Solution:

Here I discuss how to balance out both AJAX as well as SEO. That means, the site can still be crawlable with AJAX on.

Step-1: Load the AJAX content with the change in browser address bar:

  • We need to manage the address bar using client side code and add ‘#’ at the end of the URL, before the AJAX page name or parameters.
  1. If about-us page loads in AJAX, the URL will be: https://www.mysite.com/#about-us
  2. NOTE: the hash fragments are never sent to server
  • After refreshing the page, we can get the hash fragments using client side code and avail the AJAX contents

Step2: Change the URL format to crawl by the crawlers

  • ‘Googlebot’ still cannot crawl the content of ‘about-us’ page, because the crawler cannot get the content using client side scripting.
  1. Solution: it can be solved by adding an  ‘!’ after ‘#
  2. Example: https://www.mysite.com/#!about-us
  • The hash fragment (#!) works as a signal to Googlebot, to use an alternative URL (Ugly URL) for creating a snapshot of the page including the AJAX-loaded contents.

Step3: Avail the AJAX content to the crawler

  • Pretty URL: The URLs loaded on the client browser that loads dynamic content.
  1. Example: https://www.mysite.com/#!about-us
  • Ugly URL: The alternate URLs of Pretty URLs which is used by ‘Googlebot’ to get the content of the page “? _escaped_fragment_=” in place of “”#!”
  1. Example: https://www.mysite.com/?_escaped_fragment_=about-us
  • Important: There should be same page or content while accessing both the URLs.

Step4: Avail the crawlable URL format

  • Always have the Pretty URL (URL with “#!“) link on all <a href> attribute to load the AJAX content.
  • Add Pretty URLs to the XML sitemap.

How it works?

  • ‘Googlebot’ spider finds the Pretty URL “https://www.mysite.com/#!about-us” from the sitemap or from the webpage.
  • It would fetch the Pretty URL as “https://www.mysite.com/?_escaped_fragment_=about-us
  • The indexer sorts every word and stores the resulting index of words in Google database.
  • The query processor returns Pretty URLs to the user on the search results as  “https://www.mysite.com/#!about-us

Final Tidbits:

  • A URL may contain at most one hash fragment
  • Do not include links such as https://www.mysite.com/?_escaped_fragment_=about-us in the website sitemap; ‘Googlebot’ does not follow links that contain “_escaped_fragment_”
  • When “? _escaped_fragment_=about-us”is activated, you can only provide the “snapshot” of the page.
  • The crawler escapes certain characters in the fragment during the transformation. To retrieve the original fragment, make sure to un-escape all %XX characters in the fragment; specifically, %26 should become &, %20 should become a space, %23 should become #, and %25 should become %, and so on.
  • Test the crawl-ability of your app: see what the crawler sees with https://support.google.com/webmasters/answer/158587?hl=en

Why Implement Fat Model And Skinny Controller in CakePHP

Fat Model and Skinny Controller in CakePHP framework encourages developers to include as much business logic into the application models and the controllers should translate the requests, instantiating classes, getting data from the domain objects and passing it to the view.

  • This development methodology is not new but rarely adopted among the developers. Developers should focus on creating model behaviors rather than creating controller components.
  • It is a simple and powerful concept to implement is offers numerous convenient features to the developers.
  • Controller is responsible for handling & executing the actions routed through the router, it should be lightweight and agile in nature.
  • It is not about counting the lines in controller, rather putting codes in the right place.

Why Fat Model and Skinny Controller:

After developing for a while, when you look back at your code you’ll realize how hard it is to keep track of all these things. When you have a Fat Controller, it can get pretty messy even with a proper formatting.
Put as much of your code that deals with data manipulation in your Model and it solve the problem.

  • When we need some actions repeatedly in different controller, we can have them in Model.
  • We can reduce the use of requestAction() in CakePHP sites with fat models.
  • It’s easier to find out what went wrong when your methods are smaller and specific. Because, model methods are more specific than controller methods.

How To use?

Code-you should put in Controllers

  1. Usually the only functions you should have in controllers are the view functions.
  2. “before” functions, Index, Login, Signup, Add, Edit, View, Delete.

Everything else can go to your model.

Code-you should put in Models

  1. You should put the code and functions in your model that relates to Model and its data.
  2. Formatting, Retrieving, Searching, Pagination are few examples.
  3. Keep all your business logic in the models (try to write generalized methods whenever possible). Call your generalized methods from controller by passing the required parameters.
  4. Using this you will end up writing self-documented code.
  5. It is absolutely fine if your view contains some PHP code which deals with the presentation logic.
  6. You can use the Model’s callback methods like: beforeFind, afterFind, beforeValidate, beforeSave, afterSave, beforeDelete, afterDelete, onError

Skinny_Controller_logic

Example with Sample Code

Below is the example of a listing page using CakePHP paginate, search, sorting with the concept of Fat Model and Skinny Controller.

Let’s get the User list,

  • In your Controller’s Action

[sourcecode]$limit = 10;
$this-&gt;paginate = $this-&gt;User-&gt;_Pagination($limit,$_GET[‘search’]);

//To write your business logic, lets call another Model method
$listdata = $this-&gt;User-&gt;formatListing($this-&gt;paginate(‘User’));

$this-&gt;set(‘listdata’, $listdata);[/sourcecode]

  • In Your corresponding Model

[sourcecode]public function _Pagination($limit = 30,$search){
$conditions = array(‘User.is_active’=&gt;1);
if(isset($search) &amp;&amp; trim($search)) {
$search = urldecode(trim($search));
$conditions[‘User.name LIKE’] = ‘%’.$search.’%’;
}
$params = array(
‘conditions’ =&gt; $conditions,
‘fields’ =&gt; array(‘id’,’name’,’email’,’created’),
‘limit’ =&gt; $limit,
‘order’ =&gt; array(‘User.name’=&gt;’asc’,’User.created’ =&gt; ‘desc’),

);

return $params;
}
public function formatListing($userList){
$listdata = array();
foreach($userList as $ukey=&gt;$data) {
$listdata[$ukey][‘id’] = $data[‘User’][‘id’];
$listdata[$ukey][’email’] = $data[‘User’][’email’];
$listdata[$ukey][‘name’] = $data[‘User’][‘name’];
$listdata[$ukey][‘created’] = date(‘M d, Y’,strtotime($data[‘User’][‘created’]));
}
return $listdata;

}[/sourcecode]

Why FAT Model and why not Components?

  • Components should have the logic that can be shared across multiple controllers.
  • Logic should be placed inside the Components to get the data for the view. If the logic includes manipulating of the data, then it should be in a model.

Note these Check points while implementing above steps:

  • When you need to call controller methods inside a model then you are obviously doing something wrong and need to re-examine your code.
  • CakePHP will create an automatic model (instance of AppModel) when there is a table but no model file. When you create a model with wrong model file name, still CakePHP will access auto-model. Hence, it results in different behaviors and all your validations and custom functions will not work.

Too much eating may cause gaining weight, once you’re overweight; it’s too hard to lose that extra weight.
If you don’t want to end up with overweight controllers which eventually will require surgical intervention, just follow the basics and that’s Fat Model and Skinny Controller

CakePHP_fat_models

 

Suggest me if I am somewhere wrong. Any suggestions are welcome.