Showing posts with label php-framework-laravel. Show all posts
Showing posts with label php-framework-laravel. Show all posts

Friday, 10 April 2015

Get and Post request in laravel

This post is about handling GET and POST request in Laravel.But before we start you should be familiar with Routing in Laravel.Because routing is the most fundamental part in laravel.Since this is a step by step series I suggest you to read amy previous post if you find anything confusing....So let's get started.
To understand GET and POST request in laravel we will be creating a simple form with Name and Password field.We will submit this form using both GET and POST method so that things become clear :)
Before we start building our form we must create a route to our form.Open routes.php and create the following route


1
2
3
Route::get('form',function(){
 return View::make('form');
});

We have created a simple route without any parameters to form.blade.php view which holds our form
Create form.blade.php and create a  simple form




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<form method="get" action="verify_form">
 <label>Name</label>
 <input type="text" name="uname">
 <br>
 <label>Password</label>
 <input type="password" name="pass">
 <br>
 <input type="submit">
</form>
</body>
</html>

Simple HTML form with method as get and action set to verify_form.If you try to submit the form now you will get errors since we have not create a route to verify_form view.Open routes.php and create a route to verify_form.blade.php



1
2
3
Route::get('verify_form',function(){
 return View::make('verify_form');
});

Now since we have created a route to verify_form view we will just verify if our form is working by just printing the variable using $_POST[] method of php.
Open verify_form file and type in the following 2 lines




1
2
Name: {{$_GET['uname']}}<br>
Password: {{$_GET['pass']}}

Output
So our routes and all other methods are working properly...Remember GET is not the best method when you are dealing with sensitive data as you can see that uname and pass variable can be seen in the URL of the browser.
Coming to POST method the code almost remains the same except few changes like form method becomes post,Instead of get route we will create post route and we will grab the variable in verify_form using $_POST[] method
form.blade.php file




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<form method="post" action="verify_form">
 <label>Name</label>
 <input type="text" name="uname">
 <br>
 <label>Password</label>
 <input type="password" name="pass">
 <br>
 <input type="submit">
</form>
</body>
</html>

routes.php file


1
2
3
Route::post('verify_form',function(){
 return View::make('verify_form');
});

verify_form file


1
2
Name: {{$_POST['uname']}}<br>
Password: {{$_POST['pass']}}

Remember the first route remains same in both the cases..and so the output.

Tuesday, 24 March 2015

How to install laravel framework

This post is about installing Laravel framework on your computer provided that you have local server up and running.If you don't know how to install a local server on to your computer you can read my previous post on How to install a local server on computer.
So lets get started.
Open your favorite browser an search for Composer.

But before we download composer we need to allow open SSL.To do this go to your

wamp-folder/bin/php/php5.5.12(number depends on your version)/php.ini 

Open the php.ini file with notepad and search for openssl and make sure that the line

extension=php_openssl.dll

is uncommented

Go back to composer page in browser and hit the download buton


Copy the second link from the download page


Open Command prompt and browse to your Wamp folder and paste the link copied in the previous step

Now type

php composer.phar create-project laravel/laravel www --prefer-dist 

into the CMD and hit enter this will install laravel framework into www folder.

Now we are done with the installation but before we start using the framework we need to set certain permissions to the framework.Go to 

Wamp-folder/www/

Right click on app folder and open properties
Click on the security Tab
Select system and make sure that it has write permission

Now go to 
Wamp-folder/bin/apache/apache2.4.9/conf/

Open the httpd.conf file with notepad and serach for mod_rewrite and remove the # which is in front of the line



Finally we come to the end of this long process.The only thing left now is to check if all the installation was successful
Open up the browser type in localhost you should get a list of folder.
Click on public folder you should get a page like this


Thats all you have successfully installed Laravel framework on your local server 








Monday, 23 March 2015

Introduction to Laravel

What is Laravel and why should I learn Laravel?
Laravel is a framework for PHP programming language.
Think of a framework as a ordered structure within a programming language giving you the standard solutions to a typical problems.Using a framework reduces the burden of doing same thing again and again and stops you from re-inventing the wheel again and again.
Laravel framework makes easy handling of frequently used processes such as:-
  • User Registration
  • Authentication
  • Session Handling
  • Database Migration
  • Form Validation etc
Coming back to framework there are a lot of PHP framework that already exists and neither a single of them is bad,but it depends on your utility.Few frameworks are
  • CakePHP
  • CodeIgnitor
  • ZendFramework etc
The best way to know which one is better for a particular type of application is by learning all of them.So that strength and weakness of each framework is discovered.

Why Laravel?
  • Easy to learn
  • Excellent Documentation
  • Active community and forum support
But I will definitely suggest you to learn all the framework and work like a PRO :) 

What do you need to start learning Laravel?
Basic understanding of PHP language.