Question: How to use Basic Routing methods in Laravel?

Question

How to use Basic Routing methods in Laravel?

Answers 2
Added at 2016-12-30 08:12
Tags
Question

I have get some documents from Laravel Documentation.But i can't get details clearly from that. There are lot of routing methods and how to use that for my requirements? Commonly most people are using this, but what are the other routing methods?

Route::get()
Route::post()

How to pass the message or values through this Routing? Using Controller like this is a only way?

Route::get('/app', 'AppController@index');
Answers
nr: #1 dodano: 2016-12-30 08:12

Types of Routing in Laravel

There are some Routing methods in Laravel, There are

1. Basic GET Route

GET is the method which is used to retrieve a resource. In this example, we are simply getting the user route requirements then return the message to him.

Route::get('/home', function() { return 'This is Home'; });

2. Basic POST Route

To make a POST request, you can simply use the post(); method, that means when your are submitting a Form using action="myForm" method="POST", then you want to catch the POST response using this POST route.

Route::post('/myForm', function() {return 'Your Form has posted '; });

3. Registering A Route For Multiple Verbs

Here you can retrieve GET request and POST requests in one route. MATCH will get that request here,

Route::match(array('GET', 'POST'), '/home', function() { return 'GET & POST'; }); 

4. Any HTTP Verb

Registering A Route Responding To Any HTTP Verb. This will catch all the request from your URL according to the parameters.

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

Usage of Routing in Laravel

When your are Using the Route::, Here you can manage your controller functions and views as follows,

1. Simple Message Return

You can return a simple message which will display in the webpage when user request that URL.

Route::get('/home', function(){return 'You Are Requesting Home';});

2. Return a View

You can return a View which will display in the webpage when user request that URL

// show a static view for the home page (app/views/home.blade.php)
Route::get('/home', function()
{
    return View::make('home');
});

3. Request a Controller Function

You can call a function from the Controller when user request that URL

// call a index function from HomeController (app/Http/Controllers)
Route::get('/home', 'HomeController@index');

4. Catch a value from URL

You can catch a value from requested URL then pass that value to a function from Controller. Example : If you call public/home/1452 then value 1452 will be cached and will pass to the controller

// call a show function from HomeController (app/Http/Controllers)
Route::get('/home/{id}', 'HomeController@show');
nr: #2 dodano: 2016-12-30 08:12

You can get help about routing from here. Or you can get the help from Laravel.

There are 4 methods ofform data sending that you must know --

  1. Route::get for <form method="GET">
  2. Route::post for <form method="POST">
  3. Route::put for <form method="PUT"> -- This one is for updating your database, I recommend you to use laravelcollective/html, like this -- {!! Form::open(['method' => 'PUT']) !!}, but in your web browser you can find the method as POST only
  4. Route::delete for <form method="DELETE"> -- This one is for deleteing a field in your database, I recommend you to use laravelcollective/html, like this -- {!! Form::open(['method' => 'DELETE']) !!}, but in your web browser you can find the method as POST only

There are many much you have to know about Laravel Routing, like CRUD, etc.

Source Show
◀ Wstecz