Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Basic Routing methods in Laravel?

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');
like image 885
user7356399 Avatar asked Feb 05 '23 10:02

user7356399


1 Answers

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');
like image 138
K.Suthagar Avatar answered Feb 08 '23 04:02

K.Suthagar