#5 What are Routes in Laravel 5.6

The router allows you to register routes that respond to any HTTP verb:

Examples

#web.php

// This means register a route at '/' ( meaning home directory). So here / represents home directory.
// So If you write '/about' or just 'about' it would register the route at 'home_url/about'
Route::get('/', function () {
    return view('welcome'); // Load/return welcome.blade.php view file
});

#Another method
# web.php
Route::get( 'viewfilename', 'NameController@index' )
# NameController.php
function index() {
  $tasks = Task::all();
  return view( 'tasks.index', compact( 'tasks' ) );
}

#web.php
Route::get( 'viewfilename/{id}', 'NameControler@show' )
# NameController.php
function show( $id ) {
	$tasks = Task::find( $id );
	return view( 'tasks.index', compact( 'tasks' ) );
}
or
function show( Task $id ) { // If you are using in this manner then you dont need to use $tasks = Task::find( $id );, just make sure the variablename in Task $id is the same as passed in 'viewfilename/{id} .This is called route model binding, because Task $id is equivalent to Task::find( $id )
	return' $id;
	return view( 'tasks.index', compact( 'tasks' ) );
}

# This will add crud routes automatically for the controller created with --resource flag.
Route::resource( '/recruiter-dash/post-jobs', 'PostJobController' );

Some available routes

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Route::redirect('/here', '/there', 301); // Redirects to another URI
Route::match(['get', 'post'], '/', function () {
    // for multiple http verb
});
Route::any('foo', function () {
    //
});

Route::view('/welcome', 'welcome', ['name' => 'Taylor']); // Short hand of defining a route with passing data.

Add your review



Leave a Reply