Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

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 !