For people who are new to CakePHP, Cake is a PHP framework, which closely follows the Model-View-Controller format and standards. As a result of this, the application layers can be separated quite well. The only thing is that with this application, more files and folders are added but the end results are satisfactory. Among different kinds of web applications, the MVC pattern is gaining popularity. Along with this concept, Cake also follows the Convention-Over-Configuration methodology. With this, the amount of code that needs to be written gets reduced considerably.
Creating a model with CakePHP
At the very first place, Cake is installed on the local server and a database is setup followed by creation of Products table. The next step is creating a model, which will be able to correspond to the created table in the database. With Cake, the model names should always be singular, while the table names should be in plural. With this convention, you can create Product.php within the directory cake_root/app/models folder.
Once a blank new PHP file is there, you can start coding. The simplest model form in Cake is PHP class declaration. The CakeAppModel class is the parent of all models. Though not mandatory, each class has a variable – a name as it makes defining the thing easier.
The main function of a model is to grab all the available data. The model is nothing but a PHP class and the singular version of the table name. After the collection of the whole data, it is transferred to the suitable controller. The next step is the creation of the Controller.
For naming Cake Controllers, just adding ‘Controller’ to the table name will do. However, for file names, adding an underscore in between will be good enough. Therefore, the name of the controller file will be products_controller.php. The directory which will contain all the controllers is cake_root/app/controllers. You can create the file within the directory now and once that is done, coding the controller can begin.
For the working of the controller, a class has to be defined specifically, which is the child of the AppController class. Along with this, there are many other things about a controller. The main function of the controller is to access data from the model, process it and then route the same for display in ‘View’. With more data, the applications get more complex and so do the controllers. Therefore, we intend to work with the index ( ) function. This function is like any root view as in any normal web page. What we need to do is grab the data, put it inside the function and then pass it to view. The data passing is done by using basic Cake controller called set ( ). This function sets a variable to the data provided and passed to view.
The file name comes with an underscore but the name and the class name do not have the same. There is also the name variable as a precaution. Then the first function has to be defined, which is defining index ( ) function in this case. All data are collected from the products table and then a Data variable is set to pass it to view.
The page routing for Cake operates from the URL of the page. Mod_write is used for proper display of the page content. Any CakePHP application uses the decoding for page content display http:www.somesite.com/cake_root/controller/method/method argument.
At first, a view file needs to be created. In the Cake directory, there is a /app/views folder. Inside the folder, each controller has its own folder. Views are there in each controller folder, the names of which are based on the methods in the controller. So, the file will be cake_root/app/views/products/index.php. Once the file is created, it can be opened. The views are not complete HTML files. They are covered inside a layout. There are many elements that can be there in the layout.
With this view, the MVC hierarchy gets completed. The first Cake page is ready to be up and running. Browsing http://localhost/cake_root/products will give the product data along with a query table (Cake generated).
This is how the simplest page can be created with Cake PHP. If you are looking for custom cakephp web application development services, hire us at Cakephpexpert.com.