Laravel Classroom image

Rohin  Bansal / Professional / Web Technology

 
To post your Question Join Classroom
 
Notes 
Code Convention in Laravel Framework

Code Convention For Laravel Framework

Coding Conventions are the set of rules or guidelines that recommends programming style, practice and methods for a particular programming language that we have to follow while coding.

Laravel is a php framework which follows MVC architecture.

Now, the question is what is MVC architecture?

 An MVC framework is a framework designed to make utilizing the MVC architecture (software development architecture) easier. The basic goal of MVC is to reduce complexity and increase flexibility and maintainability of code and simplify the design of systems. 

It is a 3-tier architecture that Laravel follows. It separates an application into 3 logical components. Each of these components are built to handle specific development aspects of an application. These components are:

M: Model

V: View

C: Controller

Here we first need to understand the term controller as it is the bridge between model and view. So let’s understand what is controller, what it does, and what the role of controller in the architecture is.

Controller:

The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and a view port to perform actions based on that input.

          Let us see an example:

Suppose, User have to delete some data from our application. User clicks on the delete button. On clicking the button user interact with the controller and made a request to delete a record. The controller in response check credentials of the user and after that it interacts with the model and request to delete the record. The model deletes the record from the database and returns an acknowledgement to the Controller. Then the controller interacts with the view to show resultant view after the deletion of the record. And finally user can see the view with the deletion of that record.

Now we should follow certain rules while making controller in our application:

First of all the naming convention which means how to give name to a controller

 Rule:

Name of the controller:- If the name of the controller has single object then the controller name will have first letter of object as capital in plural.

Ex.: we have single object “client” -> ClientsController.

If the name includes two objects then both the objects will have first letter as capital and the second object will take plural form .

Ex.: We have two objects i.e., user and category -> UserCategoriesController.

Functions in Controller: Controllers provide a number of methods which are called actions. Actions are methods on a controller
that handles request. By default all public methods on a controller are an action, and accessible from a url. Actions are responsible for interpreting the request and creating the response. Usually responses are in the
form of a rendered view.

Naming of function or methods in Controller: If there is single object it is written in lowercase.

Ex.: public function create()

{

          body of the function

}

If there are two objects it is written as first letter of first object is small and first letter of second object in capital.

Ex.: public function anyData()

{

   body of  the function

}

 

Model: The Model is the part of MVC which implements the business logic. In simple terms, this logic is used to handle the data passed between the database and the user interface (UI).

Rules:

Naming of the Model: The first letter of model name should be capital and follow singular case.

Ex.:  Clients, client, clients

The name of the above model should be “Client

Second, most important thing is that the name of the database table should be as same as the name of the model. But in database the table name should be in Plural form.

Ex. Suppose we make a model Client we should define it as:

class Client extends Model

{

//body of the model

}

and in the database we should make table named clients.

Now the question is why we make it singular in model and plural in the database?

This is because there is one-to-one relation between the model and each client, and in database table there is one-to-many relation which means same table is for all clients.

 

View: The View component is used for all the user Interface logic of the application. Views are responsible for generating the specific output required for the request. It is user interface with which user interact. This is the presentation layer of the MVC architecture. For example, the Customer view will include all the UI components such as text boxes, dropdowns, etc. that the final user interacts with.

 Rules:

The first rule of the View component is that the all the Label name should be in proper way.

For ex.: Email_id. is not the proper way to define the label in the view of  customer registration form as underscore is not a suitable way for segregating two different objects , instead we can simply use (Space) between them.

The correct way is Email Id.

View file should not be more than 100 lines. If it is more than that then breaks them into small view files and include them in a main view file.

Important Point:

Some common parts of the code that we need to use on various views should be kept in a separate file so that we can include that particular file in all the views where we want it to be.

Note: The above point can also be applied for the Model and Controller also. Ex.:  Same mail function can be used for registration and product purchase for the customer on a shopping website. So, this function can be written in a separate file and called wherever necessary.

  

There are some points that needed to be taken care while coding

Function body should not be more than 10-15 lines. In case if it more than that it should be breaks down into smaller function and call them in the main function.

W rite the function code in a formatted way with the use of “tab” key for example:

function f1()

{

          function f2()

          {

          body of function f2

}

}

is the proper way for writing function.

Use of Comment:

Comments should be use while coding to give some knowledge about that specific part of the code.

There are two ways to mention comments in a program

  • Single line commenting
  • Multi line commenting

Single line commenting is used for declaration of the function to be used and also denoting the end of the function. 

Example -  // write your comment here.

 Multi line comment should be used above the declaration of a class showing that shows information about the parameters to be passes, the return type of the function. Also mention a comment at the end of the function that indicates the end of the function body.

Example - /**
                  * write your comment here
                 */

 
 
image
Rohin   Bansal

Skills    Laravel

Qualifications :-
Location :-Dehradun,Dehradun,Uttrakhand,
Description:-
Explore
 

  Students (0)