Convenciones de CakePHP
Somos grandes fanáticos de convención sobre configuración. Aun cuando toma un poco de tiempo aprender las convenciones de CakePHP, usted ahorrará tiempo en la marcha: siguiendo las convenciones, usted obtiene libre funcionalidad, y también se libera de la pesadilla del mantenimiento del seguimiendo de los archivos de configuración. Las convenciones también hacen un sistema de desarrollo muy uniforme, permitiendo a otros desarrolladores ayudar más fácilmente.
Las conveciones de CakePHP han sido destiladas de años de experiencia de desarrollo web y mejores prácticas. Mientras que le sugerimos el uso de estas convenciones durante el desarrollo con CakePHP, deberíamos mencionar que muchos de estos postulados pueden ser anulados, esto es especialmente útil cuando se trabaja con sistemas heredados.
Convenciones de los nombres de archivos y clases
En general, los nombres de archivo llevan el símbolo underscore "_", mientras que los nombres de las clases usan CamelCase. La clase KissesAndHugsController puede ser encontrada en el archivo kisses_and_hugs_controller.php, por ejemplo.
Sin embargo, el nombre de la clase que contiene un archivo puede no necesariamente ser encontrada en el nombre de arhivo. La clase EmailComponent es encontrada en un archivo llamado email.php, y la clase HtmlHelper es encontrada en un archivo llamado html.php.
Convenciones de Modelo
Los nombres de las clases de modelos están en singular y con CamelCase. Person, BigPerson, y ReallyBigPerson son todos ejemplos de nombres de modelos convencionales.
Los nombres de las tablas correspondientes a los modelos están y plural y usando el símbolo underscore. Las tablas subyacentes para los modelos mencionados arriba serían people [plural de persona en inglés], big_people, y really_big_people respectivamente.
Las tablas unión [Join] usadas en una relación hasAndBelongsToMany entre modelos deberían ser nombradas después de los modelos de las tablas al cual se unirán en orden alfabético(apples_zebras en vez de zabras_apples). Si su aplicación contiene este tipo de relaciones entre sus modelos Tag y Post, el nombre de tabla resultante [de la unión] sería posts_tags.
Controller Conventions
Controller classnames are plural, CamelCased, and end in ‘Controller.’ PeopleController, BigPeopleController, and ReallyBigPeopleController are all examples of conventional controller names.
The first function you write for a controller might be the index() function. When a request specifies a controller but not an action, the default CakePHP behavior is to render the index() function of that controller. For example, a request to http://www.example.com/apples/ maps to a call on the index() function of the ApplesController, where as http://www.example.com/apples/view maps to a call on the view() function of the ApplesController.
You can also change the visibility of controller functions in CakePHP by prepending controller function names with underscores. If a controller function has been prepended with an underscore, the function will not be web-viewable through the dispatcher, but is available for internal use.
View Conventions
View template files are named after the controller functions they display, in an underscored form. The getReady() function of the PeopleController class will look for a view template in /app/views/people/get_ready.ctp.
The basic pattern is /app/views/controller/underscored_function_name.ctp.
By naming the pieces of your application using CakePHP conventions, you gain functionality without the hassle and maintenance tethers of configuration. Here’s a final example that ties the conventions
- Database table: ‘people’
- Model class: ‘Person’, found at /app/models/person.php
- Controller class: ‘PeopleController’, found at /app/controllers/people_controller.php
- View template, found at /app/views/people/index.ctp
Using these conventions, CakePHP knows that a request to http://example.com/people/ 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 you’d need to create anyway.
Now that you’ve been introduced to CakePHP’s fundamentals, you might try a run through the CakePHP Blog Tutorial, an appendix at the end of this manual.

login to add a comment