Laravel Tutorial Day 2

Learning Objectives: To understand URLs to Access and Management of Laravel Routes. Routes are the way to setup some view/controller for the specific URL.

When the user opens the home page we would like to display some content from the view (view name for this example is welcome)

So first create a view name: welcome with the following code:

Welcome to Laravel Training Home Page


Now we need to set the route into the route file. Here ‘/’ means home page URL, The sample code will be as follows:

Route::get(‘/’, function () {
return view(‘welcome’);
});


Now if user wants to open http://127.0.0.1/about then we would like to fetch content from the about view The route will be as follows:

Route::get(‘/about’, function () {
return view(‘about’);
});

Make sure to create about view before writing this route information.


When you wish to collect parameter information from the URL and wish to transfer this information to view, then sthe ample code will be as follows:

compact() creates an array from existing variables given as string arguments to it.

with() allows you to pass variables to a view: Reference

Route::get(‘/service/{name}’, function ($name) {
$data = compact(‘name’);
return view(‘service’)->with($data);
});

{{ $name }} Services

Now you can able to Create various Views & Routes to set different content for different URLs.

Practice Examples:

Create a Simple Website with the following Pages:

  1. Home Page (URL: http://127.0.0.1:8000 )
  2. About Us (URL: http://127.0.0.1:8000/about )
  3. Services List (URL: http://127.0.0.1:8000/services )
  4. Service Detail (URL: http://127.0.0.1:8000/services/training )
  5. Contact Us (URL: http://127.0.0.1:8000/contactus )