Learning Objectives: To understand URL to Access and Management of Laravel Routes. Routes are the way to setup some view/controller for the specific URL.
When 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 view name: welcome with the following code:
Welcome to Laravel Training Home Page
Now we need to setup the route into route file. Here ‘/’ means home page url, sample code will be as follow:
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 about view then route will be as follow:
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 sample code will be as follow:
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 setup different content for different URLs.
Practice Examples:
Create Simple Website with following Pages:
- Home Page (URL: http://127.0.0.1:8000 )
- About Us (URL: http://127.0.0.1:8000/about )
- Services List (URL: http://127.0.0.1:8000/services )
- Service Detail (URL: http://127.0.0.1:8000/services/training )
- Contact Us (URL: http://127.0.0.1:8000/contactus )