Free Android CakePHP GMaps Articles by Bali Web Design

October 28, 2010

Redirect with exit using cakephp

Filed under: cakephp,php — admin @ 8:38 am

If you run redirect function on cakephp 1.1, don’t forget to add exit() function after redirect sintax. This thing can make security hole that confusing you. The purpose of adding exit() function after redirect sintax is to avoid php running others code after redirect function.

$this->redirect('controller/action');
exit();

However cakephp 1.2 give you default exit after redirect. The complete sintax for redirect on cakephp 1.2 is

$this->redirect('controller/action', null, true);

which the third parameter define exit = true, means terminate the script. second parameter is for setting exit status, for example 404, etc. Defaultly you only need to write

$this->redirect('controller/action');

get referer url using cakephp

Filed under: cakephp,php — admin @ 8:08 am

Sometime we need to get the referer url that access a page or url on our website. It is easy to find referer url using cakephp framework. Inside your controller or cakephp class, you can get the referer url using this sintak

Controller::referer()

Referer url is the last page or url that accessed by the user before open current page or url.

How to log your website using cakephp

Filed under: cakephp,php — admin @ 7:50 am

Cakephp is a good framework, it is a rapid web development framework for building any type of website. As a framework, cakephp has many built in feature. One of cakephp feature is build in logging system. Developer can easily log their application using cakephp.

Log function that can be accessed through your controller easily. You can save or add new log into error log or debug log, or you can make new log file. All file will be placed inside CAKEPHP_FOLDER/app/tmp/logs folder

default sintax that added to CAKEPHP_FOLDER/app/tmp/logs/error.log

$this->log("error message log");

(more…)

October 13, 2010

Captcha component for cakephp using captcha.net

Filed under: cakephp,php — admin @ 8:32 pm

Captcha is a type of challenge-response to ensure that the user is really a person that the response is not generated by computer. We usually find captcha on any form input on a website such us contact us, feedback, registration and etc. The captcha is used to protect our site from spammer.

The popular captcha is recaptcha from captcha.net website. It is free for use. Their interface or design also really nice to put on your website. You can check their sample in http://www.captcha.net/. They support both image captcha and audio chaptca.

Since i work with cakephp framework, i need to integrate this captcha into my application. I would like to share my captcha component for you. It is a simple code and easy to integrate. Here are the step (more…)

October 7, 2010

How to set foreignKey for belongsTo cakephp without id

Filed under: cakephp,php — admin @ 2:45 am

Cakephp has a good ways for handling table relationship model using AppModel association structure. It is easy to create and set relation between two or more tables on your database. Cakephp has setting for one to one, one to many, many to many relationship.

I would like to tell you about setting foreignKey for one to many relationship in cakephp that the master table doesn’t have foreignKey id. On your model, the relationship for one to many that set on child table would look like this

<?php
	class CurrencyRate extends AppModel {
		var $belongsTo = array('Currency' => array(
			'className' => 'Currency',
			'foreignKey' => 'currency_id',
		));
	}

?>

(more…)

September 4, 2010

How to redirect wordpress blog url cakephp

Filed under: cakephp,php,wordpress — admin @ 7:15 pm

Cakephp have great routing capability, i was install wordpress blog on my main website root directory. And then i need to move my blog into new folder, i put it on /app/webroot/blog/ because i build new web application on root directory using cakephp.

I use rewriting rule for my wordpress blog. The rule is http://www.balistupa.com/year/month/slug/. I want to redirect into http://www.balistupa.com/blog/year/month/slug/. I don’t want to loose my blog visitors because the blog posting has good ranking on google.

So i do setting on app/config/routes.php and add this line

Router::connect(‘/:year/:month/:slug’, array(‘controller’ => ‘pages’, ‘action’ => ‘redirect_blog’), array(‘pass’ => array(‘year’, ‘month’, ‘slug’), ‘year’ => ‘[0-9]{4}’, ‘month’ => ‘[0-9]{2}’, ‘slug’ => ‘[A-Za-z0-9-]+’));

(more…)

August 2, 2010

How to redirect /app/webroot/blog/ into /blog/ WordPress Cakephp

Filed under: cakephp,php,wordpress — admin @ 8:16 pm

At this time, a company website normally should have blog to post some business activity or related activity with website themes. WordPress is the best blog framework (CMS) that i ever used. When i was build web application under my own php framework, i wasn’t worried about integrating wordpress into my website. It would has separated setting with main application.

However, now i am using cakephp framework. And if we need to put file or folder, it need to be put inside app/webroot folder. So if i need to install wordpress blog in http://www.balistupa.com/blog, i need to put the blog folder inside /app/webroot/.

The problem is about the url, with standard setting the blog would be redirected to http://www.balistupa.com/app/webroot/blog/ not to http://www.balistupa.com/blog/. This is not look good for visitor and also for seo. Because the folder structure is to deep.

(more…)

August 19, 2009

How to do GROUP BY HAVING in CakePHP

Filed under: cakephp,php — admin @ 11:23 pm

Before June 2008, there is no clean implementation for GROUP BY. commonly people code with CakePHP will put GROUP BY sintak on conditions, something like this

$this->Product->find(‘all’, array(‘conditions’ => ’1=1 GROUP BY Product.category_id’));

Now CakePhp have a clean sintak for GROUP BY by new additional paramater on find function. We can use ‘group’ to define GROUP BY on cakephp. For example

$this->Product->find(‘all’, array(‘fields’ => array(‘Product.category_id’, ‘COUNT(Product.hotel_id) as total’), ‘group‘ => array(‘Product.category_id’)));

How if we want to have HAVING sintak on query? i ussually use this solution for new cakephp

$this->Product->find(‘all’, array(‘fields’ => array(‘Product.category_id’, ‘COUNT(Product.hotel_id) as total’), ‘group’ => array(‘Product.category_id HAVING COUNT(Product.hotel_id) > 1′)));

July 31, 2009

Forcing inner join on belongTo and hasOne

Filed under: php — Tags: — admin @ 2:13 am

By default, belongsTo and hasOne using LEFT JOIN relation between parent and child table or model. We can forcing the relationship to use either INNER JOIN or LEFT JOIN based on our need. Cakephp has add new option when binding model, name type: value is LEFT or INNER.

For example we have parent table Author and hasOne Post, we can set INNER JOIN relation by default on model class or in controller using bindModel. In this example the hasOne relation set on model class

<?php
class Author extends AppModel {
var $name = ‘Author’;
var $hasOne = array(’Post’=>array(’type’=>’INNER’));
}
?>

and running on controller

$this->Author->find(‘all’);

cakephp will generate query for author table.

SELECT `Author`.`id`, `Author`.`name`, `Post`.`id`, `Post`.`post_id`, `Post`.`title` FROM `authors` AS `Author` INNER JOIN `posts` AS `Post` ON (`Post`.`post_id` = `Author`.`id`) WHERE 1 = 1

July 7, 2009

How to change cakephp layout?

Filed under: php — Tags: — admin @ 3:30 am

The cakephp concept is MVC, Model-View-Controller. This three part will divide the area for database, business logic and design part. MVC give a good system to make a clean code for designer, database analyst and programmer. Design work will not interfere the business logic work, cakephp try to limit this thing.

A layout or html output is construct inside main layout and view for each actions. Main layout is a basic layout for your website. We can have some layout for different output type such as html, xml, rss, or etc. For each type we can have different layout as needed. For example registration page will have different layout with homepage.

By default cakephp use all layouts in folder cake\libs\view\layouts if you are not define your layout. You can override cakephp default layout by create ctp file in folder app/views/layouts. The default layout named as default.ctp. How to change this cakephp layout for certain controller action?

Simple create new ctp file inside app/views/layouts, for example mylayout.ctp. And then inside action function on controller put this code

$this->layout = ‘mylayout’;

or you can set folder path for the layout, for example we have layout file saved on app/views/layouts/xml/mylayout.ctp. you can set the path using this code

$this->layoutPath = ‘xml’;

so the complete code for changing the layout is

$this->layoutPath = ‘xml’;
$this->layout = ‘mylayout’;

« Older PostsNewer Posts »