Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • CakePHP 3.0 Conventions

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 146
    Comment on it

    CakePHP 3.0 Conventions

    Welcome to FindNerd. Today our discussion is based on CakePHP 3 Conventions. Conventions means a way in which something is usually done. In CakePHP 3 there are different set of rules which  must be followed. With the help of convention you can easily manage the code so we will talk about the different conventions available in CakePHP 3. Please have a look.

    Controller Conventions

    A) Controller Class names should be plural and end with the text 'Controller'. If we take examples then names should like that UsersController, BusesController etc.

    B) We create functions inside controller class. They are also known as actions in CakePHP.

    C) If we build a url then it should be include domain name, controller name, action name in small letters. Here is an example http://www.example.com/users/index
    In above users is controller and index is an action.

    D) If you hit the above link then it will call the index function in users controller.
    Controller class can contain multiple words like UsersAndGuestsController

     

    Database and Model Conventions

    A) Model class name should be singular but table name should be plural. Bus,UsersAndGuest are the examples for the model class and buses, usersAndGuests are the examples for the table names.

    B) If you use words from other languages then CakePHP 3 can create problem in processing. If you want to use your own language words then you need to add these rules in utility class Cake\Utility\Inflector.

    C) If you talk about the fields naming conventions then you need to put the underscore between two words like nick_name, temp_address etc.

    D) We set the foreign key as per association applied. Suppose we take an example posts table relates to users table by using hasMany association then posts table will contain foreign key like user_id. It uses the first table name in foreign key.

    E) Instead of using auto-increment in primary key you may use char(36). Whenever you create a new row using save function, it will create unique id of 36 characters.

     

    View Conventions

    A) We create view file inside folder named with controller. Suppose we have a controller named Users and index function inside it then view file will be created in folder /src/Template/Users/index.ctp

    B) We are going to combine all the conventions together. Please have look.

    Table name : users

    Table class : UsersTable at /src/Model/Table/UsersTable.php

    Entity class : GuestEntity at /src/Model/Entity/

    Controller class : Users at src/Controller/UsersController.php

    View template : index at src/Template/Users/index.ctp

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: