How to setup CakePHP
Submitted by Derick Ng on 1 May 2009 - 1:26am
AllowOverwrite is Set to All We need to make sure that the web root directory, or the directory in which we plan to keep CakePHP has AllowOverwrite set to all. We can do this by checking Apache’s main configuration file http.conf. This file should be located in the directory called conf, where we have installed Apache. In this file, there should be option for the web root directory. As the following configuration shows, the web root (which is L:/wamp/www for this particular installation) has a Directory entry in which the AllowOverwrite option is set to all. If the directory under web root, in which we plan to keep Cake, has a directory entry, we need to check that one too. <Directory “L:/wamp/www”> Options Indexes FollowSymLinks AllowOverride all Order Deny,Allow Deny from all Allow from 127.0.0.1 </Directory>
Mod_rewrite Module is Loaded We also need to make sure that the Apache is loading the mod_rewrite module. To do this, we again have to check the http.conf file. There should be a section in the http.conf file named Dynamic Shared Object (DSO) Support, where all the different modules that are loaded by Apache are listed. The modules that are not being loaded are commented out with # in the beginning of the line. So, we need to make sure that the mod_rewrite module line is not commented out. If it is, we just need to remove the # from the beginning of the line: LoadModule rewrite_module modules/mod_rewrite.so Make sure to restart Apache once you have made the above changes. As long as the above configurations are set accordingly, Apache should be running CakePHP applications without any issues at all.
Copy CakePHP to www
So the directory structure will be somewhat like this C:\wamp\www\CakePHP To test your installation go to your web browser and enter similar to this URL http://localhost/CakePHP depending on your setup.
Why CAKEPHP? 1. Almost every experienced developer hates writing same code again and again because it’s really is nothing but wastage of time. For eg:- Creating same boring add/edit/delete functions for entities and all. Other problem faced by most of the developers is the organization of code. Most of them may not understand how actually to keep their code organized or if they really need it at starting phases. But when we reach at later stages, our code becomes messy and messier. CakePHP solves this problem by its MVC structure and its attractive features. 2. MVC: I preferred it because of MVC. When I have to do something related to view, then I just go to the view folder, change it and don’t think about the rest of the coding. As well as for controller when I want to develop or change the business logic. When I have to work with db then I am working with model. 3. Good thing is that everything is separate portion. So when I am working with views, I don’t want to think about model’s coding. 4. Bad thing is the DB connection. I have some allergy about DB connection. If I wrote code in model, then with every refresh, a lot of query runs. It will be solved by writing the code in controller. 5. I prefer this because of the total control is in my hand. If I want then I can use the predefined coding structure or I can wrote it by myself using standard. 6. Component: Components are packages or group of logic that are shared and used between several controllers. If I want to copy and paste specific code between controllers then I might consider wrapping some functionality in a component and include this into the controller which is needed. I can use component in different reasons. Such as:
• • • • • • • • •
Security Sessions Access control lists Emails Cookies Authentication Request handling Image resize Paypal payment getway.
7. Behavior: Model behaviors are a way to organize some of the functionality defined in CakePHP models. They allow us to separate logic that may not be directly related to a model, but needs to be there. By providing a simple and powerful way to extend models, behaviors allow us to attach functionality to models by defining a simple class variable. That’s how behaviors allow models to get rid of all the extra weight that might not be part of the business contract they are modeling or that is also needed in different models and can then be extrapolated. 8. Helpers: Helpers are the component-like classes for the views in the application. It contains presentational logic that is shared between many views, elements, or layouts. Let any 3rd party editor such as fck editor, I can easily add this with any views. 9. PHP compatibilities: Compatible with PHP4 and PHP5. So I don’t think about the hosting. 10. CRUD: Integrated CRUD for database interaction so that we can easily control data transaction. 11. Scaffolding : Scaffolding is a meta-programming method of building databasebacked software applications. It is a technique supported by some model-view-controller frameworks, in which the programmer may write a specification that describes how the application database may be used. The compiler uses this specification to generate code that the application can use to create, read, update and delete database entries, effectively treating the template as a “scaffold” on which to build a more powerful application. 12. Code generation: I can bake the cake and generate the code automatically. Though I hate it. I always want to know everything and write the whole code by my hand. 13. Route: there is a file named route.php. By this, I can easily control the URL and the access of the project. 14. Validation: I can use the built in validation in model as well as controller. I prefer the validation in controller. 15. No apache configuration needed.
16. Coding structure and standard: I have to maintain the coding structure and standard. So if another developer who is familiar with cake standard, he can easily start coding. If anyone don’t know the CakePHP, though he can easily integrate with the project. 17. The framework also provides a basic organizational structure, from filenames to database table names, keeping your entire application consistent and logical. This concept is simple but powerful. Follow the conventions and you’ll always know exactly where things are and how they’re organized. 18. Execution time: CakePHP is much slower then coldignetor. Average execution time for cakephp is 0.31sec where coldignetor get only 0.19sec. Though the execution time can be reduced for cakephp but it will not be less then coldignetro. 19. Timely Delivery and Deployment: There’s no doubt that CakePHP will accelerate the development process. Finishing it before deadline means more debugging period. 20. Easily Upgradable Code: If we developing a structured application, then there is no issue while upgrading of an application because i already have a list of various Models in my system with their Controllers. I can easily plan the units to upgrade without going the whole PHP code again. 21. Fully Functional Code: The way applications are developed using Cake offers us to perform various tests. Best of all, it allows us unit testing to the simplest entities and their functions. For eg: If I have built a online exam system with it, I know ‘exam’ is entity of my application and the functions like exam, resultCalculation, result, examSet etc. are more easily tested. 22. Visual Tweaks after development/deployment: Since, Cake has MVC structure, all views inside Cake are always in a separate files (*.ctp) which are free of any complex PHP code. This not only makes the Visual stuff easier to implement but makes it easier to modify too. 23. Easy understandable Code: All our entities (Models), functions to these entities (Controllers), User Interface (View) are in separate files, so I will not start studying a controller if I am looking to change the Caption of some input field or any other random task. 24. SEO: In terms of SEO, I have complete freedom of my html through the layouts and views, and the routing system allows any self URLs I want, so no restrictions there and easy to SEO rajibmahmud on January 18, 2011 | Filed Under Cakephp | Leave a Comment
Cakephp Application Structure
Cakephp is based on an MVC architecture that is both powerful and easy to understood: Controllers, Models and Views guarantee a strict but natural separation of business logic from data and presentation layers. If I want to enplane by one line, then I will say that, view for the user to see, model for the connection between DB and application and controller is the big boss who controlled everything. Now I want to give some description. Controllers contains all of the logic of your application. Each controller can offer different functionality depends on the business logic; controllers retrieve and modify data by accessing database tables through models; and they register variables and objects, which can be used in views. Models are active representations of database tables: they can connect to your database, query it (if instructed to do so by a controller) and save data to the database by creating the object of the represented table. It is important to note that in order to correctly apply the MVC architecture, there must be no interaction between models and views: all the logic is handled by controllers. That means a model create a abstract object and maintain the connection with DB. Serve the purpose according to controller to retrieve, save, update and delete data. Views can be described as template files or output that present their content to the user: variables, arrays and objects that are used in views are registered through a controller. Views should not contain complex business logic; only the elementary control structures necessary to perform particular operations, such as the iteration of collected data through a foreach construct, should be contained within a view. Throw a view, a user can give input and get output from the application. Its just used for this. This architecture can greatly improve the maintainability and the organization of your site’s code:
• • •
It separates business logic from presentation and data retrieval. A site is divided into logical sections, each governed by a particular controller. When testing and debugging an application, any developer accustomed to CakePHP’s structure will be able to locate and correct errors without knowing all of the details of the code.
Controllers, models and views are stored in pre-defined directories within CakePHP’s directory structure. Here’s the directory structure that’s used:
This directory scheme must be preserved, as it is essential if the framework itself is to work. Cakephp, like ROR, believes in the importance of convention over configuration: in order to deploy an application, rather than modify dozens of different configuration files, it’s important only to place everything in its proper place; then, you can let the framework do the rest. Although this may seem worrisome for some developers, it’s a good compromise that can really accelerate the development process. CakePHP features Controller, Model, and View classes, but it also features some additional classes and objects that make development in MVC a little quicker and more enjoyable and improve efficiency. Components, Behaviors, and Helpers are classes that provide extensibility and reusability to quickly add functionality to the base MVC classes in your applications. Always a developer have to know one thing. Developer create a CONTROLLER for a specific business logic, create models for each DB TABLE and create views for each function if the view is necessary. For view, developer have to create a folder under app/view/ folder.
Controller Extensions (“Components”):
A Component is a class that aids in controller logic. If you have some logic you want to share between controllers (or applications), a component is usually a good fit. As an example, the core EmailComponent class makes creating and sending emails a snap. Rather than writing a controller method in a single controller that performs this logic, you can package the logic so it can be shared. Controllers are also fitted with callbacks. These callbacks are available for your use, just in case you need to insert some logic between CakePHP’s core operations. Callbacks available include:
• • • beforeFilter(), executed before any controller action logic beforeRender(), executed after controller logic, but before the view is rendered afterFilter(), executed after all controller logic, including the view render. There may be no difference between afterRender() and afterFilter() unless you’ve manually made a call to render() in your controller action and have
included some logic after that call.
View Extensions (“Helpers”):
A Helper is a class that aids in view logic. Much like a component used among controllers, helpers allow presentational logic to be accessed and shared between views. One of the core helpers, AjaxHelper, makes Ajax requests within views much easier. Most applications have pieces of view code that are used repeatedly. CakePHP facilitates view code reuse with layouts and elements. By default, every view rendered by a controller is placed inside a layout. Elements are used when small snippets of content need to be reused in multiple views.
Model Extensions (“Behaviors”):
Similarly, Behaviors work as ways to add common functionality between models. For example, if you store user data in a tree structure, you can specify your User model as behaving like a tree, and gain free functionality for removing, adding, and shifting nodes in your underlying tree structure. Models also are supported by another class called a DataSource. DataSources are an abstraction that enable models to manipulate different types of data consistently. While the main source of data in a CakePHP application is often a database, you might write additional DataSources that allow your models to represent RSS feeds, CSV files, LDAP entries, or iCal events. DataSources allow you to associate records from different sources: rather than being limited to SQL joins, DataSources allow you to tell your LDAP model that it is associated to many iCal events. Just like controllers, models are featured with callbacks as well:
• • • • • • •
The names of these methods should be descriptive enough to let you know what they do.
Controllers, helpers and models each have a parent class you can use to define application-wide changes. AppController (located at /app/app_controller.php), AppHelper (located at /app/app_helper.php) and AppModel (located at /app/app_model.php) are great places to put methods you want to share between all controllers, helpers or models.
Although they aren’t classes or files, routes play a role in requests made to CakePHP. Route definitions tell CakePHP how to map URLs to controller actions. The default behavior assumes that the URL “/controller/action/var1/var2” maps to Controller::action($var1, $var2), but you can use routes to customize URLs and how they are interpreted by your application. Some features in an application merit packaging as a whole. A plugin is a package of models, controllers and views that accomplishes a specific purpose that can span multiple applications. A user management system or a simplified blog might be a good fit for CakePHP plugins. rajibmahmud on September 24, 2010 | Filed Under MVC | 10 Comments
Basic requirements to use Cake PHP.
here i gives the basic requirements to use the cakephp as a development framework. 1. 2. 3. 4. HTTP Server like apachi. Enable mod_rewrite.so from httpd.conf file situated in the appachi folder. Change “AllowOverride None” by “AllowOverride All” where found. PHP 4.3.2 or greater is been needed.
5. Cakephp support different types of DB like: MySQL (4 or greater), PostgreSQL, Microsoft SQL Server, Oracle, SQLite. 6. If you change the maximum execution time as 3600 seconds and maximum memory for 128MB from php.ini, then you don’t have to think about anything else. 7. 8. Give write permission to “/app/tmp” folder. And more importent to give a big smile.
rajibmahmud on September 19, 2010 | Filed Under Tutorials | Leave a Comment
Cakephp is a popular, free and rapidly developed framwork of php. I love cakephp. When I started my career, I didn’t know even HTML. So php or Cakephp or any other framework are the same hard to me. But luckily I started with Cakephp and found that it is lot easier and huge supporting framework. It followed MVC architecture pattern and the ORM. And now I just write the prosidure sequently from the beginning to the end.
The given informaton is based on cakehph manual form http://cakephp.org . First we have to know why we use cakephp. The CakePHP framework provides a robust base for your application. It can handle every aspect, from the user’s initial request all the way to the final rendering of a web page. And since the framework follows the principles of MVC, it allows you to easily customize and extend most aspects of your application. The framework also provides a basic organizational structure, from filenames to database table names, keeping your entire application consistent and logical. This concept is simple but powerful. Follow the conventions and you’ll always know exactly where things are and how they’re organized. Here is the list according to http://cakephp.org 1. 2. 3. 4. 5. 6. 7. 8. 9. Easy to learn. Develop a huge project very first. Flexible licensing. Active, friendly community. Php 4 & php5 compatible. Integrated CRUD (Create, Read, Update & Delete) for database interaction. Application scaffolding. Code generation. MVC (Model-View-Controller) architecture.
17. Flexible Caching. 18. Localization. 19. Works from any web site directory, with little to no Apache configuration involved.