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:
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:
$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:
Thanks! Great help for beginners.
Great tips for beginners.
I also just started using CakePHP. Check out my blog articles
http://www.askaboutphp.com/tag/cakephp
CakePHP is awesome, but is a bit tough for beginners, i would recommend CodeIgniter (http://codeigniter.com). It's a great mvc framework.
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/
good simple example, it doesn't confuse a beginner like me.
Pay attention!!
You'll need this on HTTPD.CONF:
AllowOverride All
That's really helpful for me as a newbie. Thanks for posting it.
Thanks! This is a great article for beginners like me. Thanks again for the good work!!
Great, this example remove my fear againts Cakephp :)
Thanks! This is a lot easier to understand than the cakephp book.
Thanks for the great article. It is very helpful for beginner.
very good example for start cakephp thanks
it was good for understanding autoRender feature of cakephp,Not only show the code,but also write the explaination about code.
Thanks Sir,
Great example. Just what a begginers needed to get the framework showing something.
thanks.. this good for beginner like me.
Great one thanks a lot
Thank you so much, read a lot about MVC, now i could know the real meaning.
Best tutorial to start with cakePHP
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."<<<.
how to create controller n routes in cakephp???how to link .ctp file to php file??
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
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
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???
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.
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
Post a Comment