CakePHP2命名规范
?
CakePHP的命名规范相当重要,遵循CakePHP框架的命名规范,可以获得框架内置的各种功能。CakePHP2发布之后,命名规范这块与老的框架没有任何区别,本文同时适用于CakePHP 2.x和CakePHP 1.x框架。
总得来说,文件名使用下划线,类名用驼峰命名法。比如,MyNiftyClass类对应的文件名就是my_nifty_class.php。下面是不同类型的类和其相对应的文件命名方式的一些例子,
控制器类KissesAndHugsController,可以在kisses_and_hugs_controller.php文件中找到(注意文件名中的_controller)。
组件类MyHandyComponent,可以在my_handy.php文件中找到。
模型类OptionValue可以在option_value.php文件中找到。
行为类EspeciallyFunkableBehavior,可以在especially_funkable.php文件中找到。
视图类SuperSimpleView,可以在super_simple.php文件中找到。
助手类BestEverHelper,可以在best_ever.php文件中找到。
上述的每个文件都存放在相应的文件夹中。
总结:类名中,除了模型类,都需要在类命中标明该类的身份,比如Controller,Component,Behavior等。文件名中,只有控制器类需要标明身份,即在文件末尾加上_controller.
模型和数据库命名规范
模型类名使用单数驼峰命名法,Person,BigPerson以及ReallyBigPerson都是约定的模型命名。
表名使用复数和下划线命名。与上述模型相对应的表名分别是people,big_people和really_big_people。
可以使用“inflector”库检查单词的单复数形式。
包含两个或更多单词的字段名,以下划线命名,比如first_name。
在hasMany,belongsTo,hasOne关系中,外键默认由相关表加_id表示。举例来说,一个Baker和Cake之间的关系是一对多,那么在cakes表中会存在一个baker_id外键来关联bakers表(注意,这里的关联外键是单数)。对于多个单词组成的表名,像category_types,外键会事category_type_id。即表名虽然是复数,但是在其它表中存在的该表外键,要以单数形式命名。
多对多关系中一般会存在一个链接表,该链接表字段的一个命名方式是需要以单词首字母字母顺序排列,例如apples和zebras两张表,在链接表中,他们应该是apples_zebras,而非zebras_apples。
所有CakePHP模型交互的表,都需要唯一的主键来标示每一行数据。CakePHP不支持组合主键,如果你想直接操作链接表数据,可以直接使用SQL语句。
除了使用递增字段作为主键之外,还可以使用char(36)作为主键。在使用odel::save方法保存记录时,CakePHP会自动识别该字段,并使用(String::uuid)生成一个36位的字符串作为唯一主键。
控制器命名规范
控制器类名使用复数驼峰命名法,并且以Controller结尾。PeopleController以及LatestArticlesControlle都是合法的控制器命名方式。
控制器中的首个方法应该是index()方法,当请求只指定了控制器,没有动作方法时,CakePHP默认执行该控制器的index()方法。
通过在控制器方法名之前加一个下划线,可以更改该控制器方法的可见范围。如果控制器方法前有一个下划线,这个方法就只能在内部使用,而无法通过浏览器访问。
控制器类名与URL地址
单个单词的控制器能够非常容易的被映射到小写的URL地址上。例如,ApplesController可以通过http://example.com/apples访问。
多词控制器的访问地址,可以是该控制器名的任何曲折形式。
- /redApples
- /RedApples
- /Red_apples
- /red_apples
都能指向到RedApples控制器的index方法。不过,命名约定规定,url地址应该是小写单词加下划线组成。因此,/red_apples/go_pick是请求RedApplesController::go_pick方法的正确形式。
视图命名规范
视图模板文件通过调用他们的控制器方法命名,以下划线分隔多词。PeopleController类中得getReady()方法,会自动关联/app/views/people/get_ready.ctp视图模板。
命名方式就是/app/views/
总结
数据库表名:people
模型类:person,路径app/models/person.php
控制器类:PeopleController,路径app/contollers/people_controller.php
视图类:app/views/people/index.ctp
使用这种命名约定,CakePHP能够将http://example.com/people/映射到PeopleController控制器的index()方法,Person模型可以直接在该控制器中使用,并且该模型会自动与people表绑定,然后输出视图。所有这一些不需要配置,只要你遵守CakePHP的命名约定即可。

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software