9854-888-021 New York, NY
Posted on / by admin / in Cakephp, Controller, PHP

CakePHP Controller

Now let’s move further. the next part is controller. Controller has logics (some times reffered Business logic in Java). In controller you can write any logical code, such as DB queries, calculations and every thing which you can do in PHP. But remember to use cake functions to ‘Make Life Simpler’. The controller should be in app/controllers directory.

You should name controller carefully. The name of controller should be ****_controller.php (replace ****) with your controller name. You should take care about controller name because when you run any page (code) of controller you have to type http://server.com/(Controller)/(Page/Function Name).

Controller class extends AppController. If you want to add some global functions for all controllers, you can write them in cake/app_controller.php. Now lets create our first controller

class UserController extends AppController {

var $name = ‘Users’; //Name of controller
#var $layout = ‘upload’; //Default Layout for this model
//in case you want to use different layout (I will cover Layouts in next posts)
#var $uses = array(‘User’); //If you want to use more then one table (Model) then add in uses array.
#var $components = array(‘Email’); //Components to be used in this controller (I will cover components in next posts)
function index() // Default function, when called show index page. { }
}

Leave a Reply