Learn Laravel 5 routes

Learn Laravel 5 routes
Rate this post

|————————————————————————–
| Application Routes
|————————————————————————–
|
| Here is where you can register all of the routes for an application.
| It’s a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

// call simple get method by default parameter with welcome view page
Route::get(‘/’, function () {
return view(‘welcome’);
});

// call simple get method by hello parameter with simple text view
Route::get(‘/hello’,function()
{
return “hello india”;
});

// this will match parameters are coming with get or post, we can pass single method or multiple methods like get or post, delete
Route::match([‘get’, ‘post’], ‘/’, function () {
return “get or post”;
});

Route::any(‘/’, function () {
return “any method will capture”;
});

//This is example of single parameter pass
Route::get(‘/country/{country}’,function($country){
return “Country Name is: “.$country;
});

//This is example of multiple parameter pass
Route::get(‘/country/{country}/city/{city}’,function($country,$city)
{
return “Country Name is: “.$country.”
City name is: “.$city;

});

//Optional Parameters
Route::get(‘user/{name?}’, function ($name = null) {
return $name;
});

Route::get(‘user/{name?}’, function ($name = ‘John’) {
return $name;
});

//Regular Expression Constraints, so name will take alphabet only
Route::get(‘/name/{name}’,function($name){
return $name;
})->where(‘name’,'[A-Za-z]+’);

Route::get(‘user/{id}’, function ($id) {
return $id;
})
->where(‘id’, ‘[0-9]+’);

//Named Routes
Route::get(‘user/profile’, ‘[email protected]’)->name(‘profile’);

//Generating URLs To Named Routes
// Generating URLs…
$url = route(‘profile’);

// Generating Redirects…
return redirect()->route(‘profile’);

no replies

Leave your comment