Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Wednesday, October 7, 2020

October 07, 2020

Understanding Git Commands [Simple & Quick]

As a developer, you may feel that any changes that you made on your project should be documented somewhere. Especially, when working in a team one must know who made what changes at which time etc. 




Moreover, since many developers are working on the same project it may result in a conflict or there may be some project sync problems. Fortunately, a solution to these problems exists in the form of "Version Control System".


As the name suggests, A version control system is a software that has the responsibility to control/manage the versions i.e changes to the system.


One of the most popular version control system which is used all over the world is "GIT".


Git is a version control system that operates on different commands. In this article, we will discuss some of the important GIT commands one by one and will see how they can be used in your project.

So here we go.


GIT COMMANDS:

Before starting to talk about GIT commands, I will present an analogy that will help you to understand them well.


"Suppose you have clothes scattered on a bed. You have to put them in different baskets according to their category, then take the baskets to the nearest Locker and put the clothes from each basket into the locker".


1. GIT INIT:

Project on which git works is called a repository. To make your project a git repository you have to initialize git on it.


git init:  This command will initialize your project as a git repository. 


After you execute this command,  A hidden folder named .git will be created in the project root folder.  This folder will contain all the files required to make git work on the current project.


This command is like telling git:" Hey, Git initialize my project as a repository so you can start to track files in this project"


GIT will say: "Ok, let me create a git folder in your root folder to start tracking"


This git repository is also called " Git Working Directory"


2. GIT ADD:


We have told git to make our project as a git repository but we haven't told git about on which files the changes should to tracked. This git command is like putting clothes on the bed without being placed into the basket.

To tell git about which file changes should be tracked, we have to put them in what we called a "Staging Area". Any file on which changes needed to be tracked should be in a staging area. Think of a staging area as a bed where clothes needed to be placed.

Now To add files in the staging area, write this command: git add file-name

But if we want to track all files on a project instead of a single file then use this command: git add  .

This command will tell git to add all files of the project into the staging area.

Now after executing the above command we have placed clothes on the bed. It's time to put clothes in their respective baskets like shirts into shirt basket etc.


3. GIT COMMIT:

Commit simple meaning is a change. Remember git goal is to track/revert changes? To do this we have to make sure a change is saved so we can revert it whenever we want.


So to do this we use this git command. It will create a copy of this change. This is like scattered clothes on bed but we haven't put any clothes in the basket yet.

Command: git commit -m "Something Changed"

After executing the above command, our change will be saved. This is like clothes on the bed which is being placed into their respective baskets.


4. GIT PUSH:

If a project is located on a remote server then we have to make sure our local changes should be sync with the remote directory.  Here local changes mean all commits of a project as described above.

We have to push any changes made in the local directory to the remote directory. Here changes mean all commits of the project.

Before moving the commits to remote, we have to tell git about the location of the server.

To do this use the below command:

Command: git remote add origin https://github.com/Username/app.git

As the GitHub is the most popular git repository, we are using it as our remote location. But If your project lies on different hosting then you should be able to find the option of "Git Version Control" from your Cpanel where you can find the remote address of your project.

Now we have added the remote address, it's time to place the clothes from the basket to the locker i.e push project changes to remote.

Use the command: git push


4. GIT PULL:

To fetch the latest commits from the remote server use this command: git pull

Conclusion:

Still, confused? Well, there is a solution in the form of GUI for those who don't want to fight with git commands. The most popular one is the Github Desktop.

For those who prefer to write Git commands can download Git Bash

I hope these basic concepts about the working of GIT commands will help you to better work with your team and to better manage your projects. 


HAPPY CODING!


Tuesday, March 19, 2019

March 19, 2019

What is Composer in PHP/Laravel? How to Install PHP Composer


Most of you already heard a term "Composer in PHP" but only few of you actually know what actually it is , composer advantages, how it works , how to install composer and finally it’s different commands like composer install, composer update. Well don’t worry as this article going to answer all your questions regarding composer. Moreover, you will also see the practical example of using composer in Laravel.

composer-in-php-laravel


Before understanding about composer in PHP we have to first know about what is "Dependency management". Dependency management simply means managing the dependencies on which the project depends.

Suppose you are working with images and you need a library to manipulate the images in a better and easy way like creating, editing images, adding watermarks on it etc. To achieve this task we can use PHP library named "Intervention", We have to download the library and manually adding this in our code.

For small projects adding libraries manually is not a huge task, But if you have large projects then manually adding them is not a right way to do. Moreover, if you are working with PHP framework like Laravel then you know that Laravel uses different libraries for different purposes i.e authentication etc. Because of the lot of dependencies on which Laravel depends, Recommend way of installing laravel is through  "Composer".

Composer is a dependency manager software which manages the PHP project dependency on a fly. The project should have composer.json file where all the required dependencies are listed, Composer install them automatically.

To use composer in PHP projects, You have to download and install composer in your computer first. Here are the steps:

1-First download the composer

2-Now open the executable file to install it
What is Composer in PHP/Laravel? How to Install PHP Composer

3-Make sure to check developer mode as this will install the composer globally and you don’t have to add the composer path in environment variable to use it.
composer-php


4-Now you have to choose the command line PHP executable file.

installing-composer-php


5- If you are using xammp then click browse and navigate to the root path of your xammp installation and open the PHP folder where you will find a executable file named php.exe, Select it and click next

composer-in-php-laravel
composer-in-php-laravel


6- Now click next without checking proxy server as generally it is not needed (unless you are using some sort of VPN)

composer-in-php

7-Now click finish and you are done with the installation of composer.

Using Composer in PHP/Laravel:


Now we will see a little quick demo on using composer in laravel.

1-Installing any package with composer is just a game of one line, enter one command in command prompt hit enter and from here composer will do the rest of the magic.

2-There are two ways to install Laravel using composer

First way:

1- For installing laravel navigate to the folder where you want to install laravel using cmd.

2-We will use the following command to install laravel:  composer create-project laravel/laravel {directory} {version}

4-This command will simply clone the laravel from the GitHub repo directly to your PC.

5- So in my case I ran following command: Composer create-project laravel/laravel  laravel-practicles  "5.7.*"

6-I first navigate to my htdocs folder then I ran the above command.

7-This command installs the laravel in laravel-practicles folder.

Second way:

1-First, download the laravel installer using composer: composer global require laravel/installer

2-This command will install the laravel package globally in our local system

3-Now to create project for laravel again and again we can just ran following command: laravel new project-name

4-The above command will create a fresh laravel  directory in the specified folder

5-This method is fast and i personally like this.

As you know laravel has many dependencies but since we are using composer to install laravel, All the required dependencies will be automatically installed by composer.

This was the just glimpse of how powerful PHP composer is, I recommend you to use it in your projects when installing any external library as it will be life savior for you.

Let me know in comments if you have any questions/issues regarding composer in PHP, Thanks.


Sunday, November 18, 2018

November 18, 2018

Strategy Pattern (Design Patterns) in PHP


Aj is article me ham parhen gen Strategy Pattern ke bare me ke Design Patterns kia hoten hain? Strategy Pattern kia hay, Uske ilawa Php Design Patterns ke bare me kuch general concepts aur phir ham sikhen gen ke PHP me kis tarah ham is strategy pattern ka use krskte hain.
Strategy-Pattern-PHP

Tuesday, October 30, 2018

October 30, 2018

How to Create Simple CV Generator Using mPDF Library in PHP

In this article we will learn How to Develop a Simple CV Generator in PHP using mPDF library. We will also use CSS & JS framework bootstrap and vue.js in our project. So without any delay, let’s start

Intro:
We will develop a CV generator web app which allows user to generate a PDF CV after user enter his details i.e name, skills, experience bio and click generate button. You can checkout the scshot below:
How to Create Simple CV Generator Using mPDF Library in PHP


Thursday, October 4, 2018

October 04, 2018

How Access and Handle Form Data in PHP (POST And GET)


Aj is article me me apko batao ge ke PHP me forms ko kis tarah handle kia jata hay, super global array $_GET aur $_POST kia hain? Forms ki value ko kis tarah PHP me process kia jata hay etc.

form-handling-php

What is HTTP?:

Jaisa ke apko pata hoga ke HTTP ek protocol hay jis ko use krte we client aur server ek dusre se communicate krte hain.

Jab bhi ham kisi server se communicate krte hain to hamari ek HTTP request server ke pas jati hay aur server ka response hamare pas HTTP response ki surat me ata hay.

Jaise Jab ham kisi website ko access krne ke liye us ka URL address bar me likh kr enter krte hain to phir us website ke server ke pas ek HTTP request jati hay jis kay response me hamare pas us website ka ek page ajata hay.

Lekin HTTP request sirf website ko access krne se nh jati balke  ham kisi form ko fill karke submit karte hain to bhi ek HTTP request server ke pas jati hay, Jis me ham server se kehte hain ke is form ko submit karne par response me hamen koi web page de do.

PHP Form Handling:

Ab q ke ham form submit karhe hain to iska matlab form ke data par koi na koi processing hogi (E.g data ko database me insert krna) aur wo php me hogi to isliye ham server se request krte hain ke form submit hone per kisi PHP file ko execute krdo.

PHP file ko request krne ke ilawa ham apne data ko server per sent bhi karte hain yani ham form ko submit krte we do kam karte hain:

1-Request PHP file
2-Sent form data to server

Uper wale donu kam krne ke liye hame kuch form ke kuch attributes samjhne ki zaroat hay.
1-Action
2-Method
3-Name

Action:

Action ek attribute ha jo ham form tag me use krte hain aur is me ham us file ka name likthe hain jo hame apne form key submit hone par chaiye, Jese below example me jese hi hamara form submit ho to add.php ki file ka code execute krke server hamen us ka ouput bheje.

Method:

Form ko submit krne ke sath sath hamen form ka data bhi chaiye hoga take ham us data ko add.php wali file me access krke database me insert krsken.

Ab sawal paida hota ha ke add.php wali file ko kia pata ke knsa data ko database me add karna hay yani jo data form se aya hay usey ham add.php wali file me kesey access karen?

To iska hal ye hay ke ham form ko submit karte we add.php file ko server se request krne ke sath sath apne data ko server ke pas bhej de gen take add.php wali file me ham us data ko access krke database me insert karsaken.

Ab server ke pas ham apne form ke data ko do tariko (Methods) se bhj skte hain.

1-GET- Is method ke through hamare form ka data URL me append hojae ga jisen ham add.php file me access krskte hain.

2-POST- Is method ke through hamara data URL me append nh hoga balke HTTP Body ka hissa ban kr server ke pas chala jae ga jisey ham phir add.php wali file me access krskste hain.

Form ke data ko kis method ke through bhjna hay uske liye ham form tag ke andar hi ek aur attribute "Method" ka use karen gen jis ki value either POST hogi ya GET hogi.

Standard aur security point view se behter hay ke form ke data ko POST method ke through hi bhja jae q ke form me aksar hamare pas password wagera jesa sensitive data hota hay jo visible nh hona chaiye aur GET method ke through to har data URL me append hojata hay, jo visible hota hay.

Name:

Ab hamne form ke data ko access krna hay add.php wali file me aur suppose ham POST method ke through form data ko server ke pas bhj rhe hain.

To ab hamen form ke har element me jo value user ne input ki wo add.php file me chaiye, To us kay liye ham form ke har element me "Name" attribute use karen gen aur usey koi  name de den gen aur phir us name ko use krte hue ham form ke har element ko add.php wali file me refer Karen gen.
 

Super Global Array $_POST and $_GET:

Super Global Array ek associative array hoti hay jis ki value ham kisi bhi page per access krskte hain.

Agar hamne form me method POST use kia hay to hamare form ka data $_POST array me store hoga aur agar GET method use kia hay to $_GET me store hoga.

Ab hamne jis method ke through server ke pas form ka data bheja hoga to form ke data phir us method ki Super Global array me key=> value pair ki surat me store hojae ga, Key me har element ko hamne jo name assign kia tha wo store hoga aur value me us element me jo value input ae hay wo store ho gi.

Jese neeche example me hamne input type=”text” ko name=”email” diya hay, Ab is input ki value ko add.php me access krne ke liye ham is tarah likhe gen $_POST[‘email’].

Ab ap do files create karen ek file ka name rakhen “index.php” jis me hamare pas form ae ga aur us ke elements ae gen aur form aur element ko ham muktalif attribute assign Karen gen jinko upper hamne discuss krliya hay


1.  <!DOCTYPE html>  
2.  <html>  
3.  <body>  
4.      <form action="add.php" method="post">  
5.          <input type="text" name="email">  
6.          <input type="passsword" name="pass">  
7.          <input type="submit" name="submit">  
8.      </form>  
9.  </body>  
10. </html>  

Ab jese hi user apna email aur pass likh kr submit kare ga then server add.php me likhey gen code ko execute kre ga  aur form ka data database me insert hojaye ga, Ab dusri file create karen aur us ka name rkhen “add.php” .


1.  <?php  
2.      $email=$_POST['email'];  
3.      $pass=$_POST['pass'];  
4.      echo $email;  
5.        
6.      //Database Related Code  
7.  ?>  

Ab add.php me ham form ke data ko super global array( $_POST[] ) use krte we hue access karen gen aur un ki value email aur pass ke variable me store karen gen, Ab ham in donu variable me mojud value ko database ka code likh kr easily database me insert krskte hain, Filhal samjhane ke liye mene email ki value ko echo krdia hay.

Conclusion:

I hope apko samajh agya hoga ke kis tarikey se ham PHP me forms ko handle krskte hain, Form ki values ko access krke un per muktalif operation perform krskte hain, Lekin yad rkhiye programming practice ka name hay agar practice nhi to concepts ko koi kam nhi, Isliye ap khud se practice zaror ki jiye ga. 

Ap practice ke tor per ek registration form banaskte hain jis me user apni muktalif details enter kare aur wo database me store hojae.

Agar apko koi bhi help chaiye to is article per comment karen me apki bharpoor tarikey se madad krne ki koshsih krun ga.

Thanks and remember Sharing is Caring !