/path_to_document_root /app /lib /plugins /vendors .htaccess index.
php README
CakePHP Folder Structure
After youve downloaded and extracted CakePHP, these are the files and folders you should see: app lib vendors plugins .htaccess [Link] README
Youll notice three main folders: The app folder will be where you work your magic: its where your applications files will be placed. The lib folder is where weve worked our magic. Make a personal commitment not to edit files in this folder. We cant help you if youve modified the core. Finally, the vendors folder is where youll place third-party PHP libraries you need to use with your CakePHP applications.
The App Folder
CakePHPs app folder is where you will do most of your application development. Lets look a little closer at the folders inside of app. Config Holds the (few) configuration files CakePHP uses. Database connection details, bootstrapping, core configuration files and more should be stored here. Controller Contains your applications controllers and their components. Lib Contains 1st party libraries that do not come from 3rd parties or external vendors. This allows you to separate your organizations internal libraries from vendor libraries.
Locale Stores string files for internationalization. Model Contains your applications models, behaviors, and datasources. Plugin Contains plugin packages. tmp This is where CakePHP stores temporary data. The actual data it stores depends on how you have CakePHP configured, but this folder is usually used to store model descriptions, logs, and sometimes session information. Make sure that this folder exists and that it is writable, otherwise the performance of your application will be severely impacted. In debug mode, CakePHP will warn you if it is not the case. Vendor Any third-party classes or libraries should be placed here. Doing so makes them easy to access using the App::import(vendor, name) function. Keen observers will note that this seems redundant, a s there is also a vendors folder at the top level of our directory structure. Well get into the differences between the two when we discuss managing multiple applications and more complex system setups. View Presentational files are placed here: elements, error pages, helpers, layouts, and view files. webroot In a production setup, this folder should serve as the document root for your application. Folders here also serve as holding places for CSS stylesheets, images, and JavaScript files.
A Typical CakePHP Request
Weve covered the basic ingredients in CakePHP, so lets look at how objects work together to complete a basic request. Continuing with our original request example, lets imagine that our friend Ricardo just clicked on the Buy A Custom Cake Now! link on a CakePHP applications landing page.
Flow diagram showing a typical CakePHP request Figure: 2. Typical Cake Request. Black = required element, Gray = optional element, Blue = callback 1. 2. Ricardo clicks the link pointing to [Link] and his browser makes a request to your web server. The Router parses the URL in order to extract the parameters for this request: the controller, action, and any other arguments that will affect the business logic during this request. Using routes, a request URL is mapped to a controller action (a method in a specific controller class). In this case, its the buy() method of the CakesController. The controllers beforeFilter() callback is c alled before any controller action logic is executed. The controller may use models to gain access to the applications data. In this example, the controller uses a model to fetch Ricardos last purchases from the database. Any applicable model callbacks, behaviors, and DataSources may apply during this operation. While model usage is not required, all CakePHP controllers initially require at least one model. After the model has retrieved the data, it is returned to the controller. Model callbacks may apply. The controller may use components to further refine the data or perform other operations (session manipulation, authentication, or sending emails, for example).
3.
4.
5. 6.
7.
Once the controller has used models and components to prepare the data sufficiently, that data is handed to the view using the controllers set() method. Controller callbacks may be applied before the data is sent. The view logic is performed, which may include the use of elements and/or helpers. By default, the view is rendered inside of a layout. Additional controller callbacks (like afterFilter) may be applied. The complete, rendered view code is sent to Ricardos browser.
8.
Database table: people Model class: Person, found at /app/Model/[Link] Controller class: PeopleController, found at /app/Controller/[Link] View template, found at /app/View/People/[Link]
Using these conventions, CakePHP knows that a request to [Link] maps to a call on the index() function of the PeopleController, where the Person model is automatically available (and automatically tied to the people table in the database), and renders to a file. None of these relationships have been configured by any means other than by creating classes and files that youd need to create anyway.
With your model defined, it can be accessed from within your Controller. CakePHP will automatically make the model available for access when its name matches that of the controller. For example, a controller named IngredientsController will automatically initialize the Ingredient model and attach it to the controller at $this->Ingredient: echo"<pre>"; print_r($final_array); exit();.