Thursday, May 15, 2008

CakePHP: Hello World!

CakePHP is an MVC framework. Model, view, controller are the main components. Generally speaking, the model is an object and is used to operate database. You should employ it to communicate with database, like inserting, deleting, querying. The view is as its name. It is used to display the result. Usually it contains lots of HTML but few PHP code. The controller is employed to handle HTTP requests and do some logic. The controller is central in an MVC pattern. It calls the model to get data from database, then do some work, and sends the result to a view to display.

Let's have a look at CakePHP. I will use CakePHP to write a "hello world" program. My CakePHP is located in D:\xampplite\htdocs\cake. In it there are four directories: app, cake, docs, vendors. "app" is the working directory where we should save our code. Now let's write some codes.

<?php
class TestersController extends Controller{
var $uses=null;
var $autoRender=false;
function sayHello(){
echo 'Hello, CakePHP!';
}
}
?>


As you see, the name of the controller class is TestersController. i.e. The controller's name is Testers. According to the CakePHP convention, the controller's name should be plural form. You must save it as "testers_controller" in app\controllers so that CakePHP can find the controller class.

Before you run it, please make sure that mod_rewrite is on. Check your Apache http.conf, find the line like "LoadModule rewrite_module modules/mod_rewrite.so". If it is commented, just uncomment it (remove "#"). Now start or restart (if you modified http.conf) Apache server. Enter http://localhost/cake/testers/sayHello in your browser's address bar. You can see the result if no problem happens:
cakephp_helloworld


Now let me explain the program in detail. In the TestersController class I define two variables: $uses and $autoRender. $uses specifies which models the controller can use. I set it "null" so that it don't use any model. If I omit it, CakePHP will look for the model "Tester" automatically. That will produce an error because I haven't created the model class. See below:
cakephp_missing_model

$autoRender=false makes CakePHP not search for the view for TestersController::sayHello() because I haven't create the view. If it is omitted, a missing view error will occurred.

The function sayHello() is called "action" in the controller class. That's to say, A controller may have several actions to handle different tasks, and each action is just a function in the controller class. Controllers and actions are mapped into URL. For example, when we open http://localhost/cake/testers/sayHello, CakePHP will find TestersController class and execute sayHello() function automatically. We can call any action by typing different URLs.

That's all. In this example I just used controller to write a "hello world" program. It is just beginning. There are many features in CakePHP. I hope my articles will be helpful for you.

Update: To simplify the example, I use "echo" statement in the controller. That's a bad style. In fact, we should use "view" to show data or result. If you are interested, see Working with View.

24 comments:

Anonymous said...

Thanks! Great help for beginners.

Anonymous said...

Great tips for beginners.

I also just started using CakePHP. Check out my blog articles

http://www.askaboutphp.com/tag/cakephp

Anonymous said...

CakePHP is awesome, but is a bit tough for beginners, i would recommend CodeIgniter (http://codeigniter.com). It's a great mvc framework.

Anonymous said...

Your post completely defies the MVC structure. To accurately portray a this application, you should have created a view for the controller, created the model for demonstration, but set useTable = false in the model instead of declaring it in your controller.

This example will do nothing but confuse a beginner because once a new developer with Cake wants to do anything besides say "hello world", you've confused the hell out of them.

If you really want to learn Cake from the beginning, go to http://book.cakephp.org/

Anonymous said...

good simple example, it doesn't confuse a beginner like me.

Anonymous said...

Pay attention!!

You'll need this on HTTPD.CONF:

AllowOverride All

Souphorn said...

That's really helpful for me as a newbie. Thanks for posting it.

Anonymous said...

Thanks! This is a great article for beginners like me. Thanks again for the good work!!

Anonymous said...

Great, this example remove my fear againts Cakephp :)

Chad said...

Thanks! This is a lot easier to understand than the cakephp book.

Unknown said...

Thanks for the great article. It is very helpful for beginner.

perumal said...

very good example for start cakephp thanks

Sonu said...

it was good for understanding autoRender feature of cakephp,Not only show the code,but also write the explaination about code.

Thanks Sir,

pablo.liz [lithium] said...

Great example. Just what a begginers needed to get the framework showing something.

Firmansyah said...

thanks.. this good for beginner like me.

Prasad said...

Great one thanks a lot

Anonymous said...

Thank you so much, read a lot about MVC, now i could know the real meaning.

Best tutorial to start with cakePHP

janlazar said...

I agree, it's good article, just maybe i would kindly ask you to change name of the file "testers_controller" to "testers_controller.php" in sentence: >>>"You must save it as "testers_controller" in app\controllers so that CakePHP can find the controller class."<<<.

VALMIKI GROUP said...

how to create controller n routes in cakephp???how to link .ctp file to php file??

VALMIKI GROUP said...

plz tell me step by step i m beginer who dont know anythg abt cakephp...plz help me in executing my hello world pgm.....i m nt getting it....plz

VALMIKI GROUP said...

after we install cakephp....wat to do??? wat r basically default file already with cakephp...n how to create our own file n exe .....plzzzzzzzzzzzzzzz

VALMIKI GROUP said...

i enguire a new problem...if i type http://localhost/cakephp...it is giving hello world as i made changes to view>layout>default.php file????
wat to do???

eternalsoft said...

Thank you for sharing this useful blog on CakePHP. You have provided very important information on this blog. Eternal Web provide high quality CakePHP Application Development Services and Custom CakePHP Solutions. Kindly contact us on.

Interview Sortout said...

Thanks for sharing such a nice information with us on PHP and related topics. Very useful lines and to the point. I appreciate your effort, keep sharing such wonderful things.

In How many ways we can use Model in CakePHP