4

I am a beginner in laravel i am shifting from codeigniter to laravel so i dont have the concepts of routes.Can any one tell me what is the difference between a post and get route in laravel 5.

Basic GET Route

Route::get('/', function()
{
    return 'Hello World';
});

Basic POST Route

Route::post('foo/bar', function()
{
     return 'Hello World';
});

Is their any disadvantage or benefit or if i use both of them at same time And when should i use both of them what happen if i pass parameter to them when i am using them at the same time.

Route::match(['get', 'post'], '/', function()
{
    return 'Hello World';
});
  • 1
    It really comes down what you want to achieve. Generally speaking, HTTP GET requests are used for reading things, while HTTP POST requests are used for creating/uploading things. I recommend reading up on HTTP verbs and what they are meant for (GET and POST are not the only ones...) – alexander.biskop Mar 24 '15 at 18:12
  • @alexander.biskop if we want to fetch data from data base using id(P.K) which should i use.from what i read it from laravel 5 doc we can pass parameters to both get and post request.? –  Mar 24 '15 at 18:15
  • 1
    It depends on the context, but in general, I'd say you should use GET. However, this is not really related to laravel or any other specific framework/programming language/platform. It's just basic HTTP stuff. As I said, I recommend reading up on the topic. E.g. start here: http://www.restapitutorial.com/lessons/httpmethods.html – alexander.biskop Mar 24 '15 at 18:18
  • I am sorry to disturb you again what i need to know that do i have to make a new route for every jquery ajax request for example i have $.ajax request for save,edit,delete,fetch then will i have to make 4 routes in routes.php file @alexander.biskop –  Mar 24 '15 at 18:22
  • 1
    Yes, in that case you'd create a GET route for fetching/reading, a POST or PUT route for creating/editing things, and a DELETE route for deleting. – alexander.biskop Mar 24 '15 at 18:26
  • Thnx @alexander.biskop it was really helpfull.. –  Mar 24 '15 at 18:27
  • @UsamaLucky Instead of creating 4+ routes each you can achieve the same with one resource route. Take a look at [RESTful Resource Controllers](http://laravel.com/docs/5.0/controllers#restful-resource-controllers) in the docs. – lukasgeiter Mar 24 '15 at 18:44
  • @lukasgeiter can you give me example of it to make it more clear how can i do that in this situtation... –  Mar 24 '15 at 18:48
  • 1
    @UsamaLucky Unfortunately I don't have the time to write an answer. Maybe someone else will. Resource routes are routes that listen to standard set of requests. If you had `Route::resource('foo', 'FooController`) `GET foo` would call the `index` method in your controller and show all resources. `GET foo/1` would call the `show` method with the first parameter `1` and so on... All actions are listed in the documentation – lukasgeiter Mar 24 '15 at 18:54

1 Answers1

11

It's matter of HTTP protocol. Simply said, GET is usually used for presenting/viewing something, while POST is used to change something. For example, when you fetching data for some user you use GET method and it'll look something like this:

Route::get('users/{id}', function($id) {
    $user = \App\User::find($id);

    echo "Name: " . $user->name . '<br>';
    echo "Email: " .  $user->email;
});

while with POST method you create or update user data (when user submits form you send POST request to this route):

Route::post('users', function() {
    try {
        \App\User::create([
            'name'      => \Input::get('name'),
            'email'     => \Input::get('email'),
            'password'  => bcrypt(\Input::get('password'))
        ]);

        return Redirect::intended('/');
    } catch(Exception $e) {
        return $e->getMessage();
    }
});

It's just a simple example, but I hope you can see the difference.

mirzap
  • 445
  • 1
  • 6
  • 10
  • Thnx bro your answer was really helpful but can you tell me for what is this code is use for \App\User::create() –  Mar 24 '15 at 19:02
  • \App\User is a model, it comes with default Laravel 5 installation. create() method is a part of Eloquent ORM, and it's used to save new model and it returns the instance of new model. Take a look at official documentation page: http://laravel.com/docs/5.0/eloquent There is a bunch of tutorials how to use Eloquent, just google it ;) – mirzap Mar 24 '15 at 19:09