Hi, folks, we have successfully mastered MVC in the last class, if you missed it go back here.
We have talked about routes and where they can be found in the project, will do a quick freshened up in routes.
Laravel routes simply accept a URI and a Closure, providing a very simple and expressive method of defining routes:
Before we start using a route, take note of the following :
Available Router Methods
- get - you use get when you want to get resources from the database
- post - you use post when you want to create resources for the database
- put - you use put when you want to update a specific resource from the database
- delete - when you want to delete a specific resource from the database
- patch - you use patch when you want to update bulk resources from the database
Example of get route
Route::get('/welcome', function () {
return 'Hello World';
});
Let's break things down here
get -> method type of request
/welcome -> destination of the request, the welcome can be of any webpage like contact-us etc
function -> function to be executed
return -> to render hello world
Testing the route
from your terminal on your project root folder or navigate to your project root folder and start your laravel server using
php artisan serve
or
php artisan server --port = 8090
your server will start at
Starting Laravel development
server: http://127.0.0.1:8000
open your browser and type in http://127.0.0.1:8000/welcome you will see Hello World which was the data that we return from a welcome webpage, for now, we will keep using a get request route, others will come in as we progress in this class.
Example 2
Route::get('/price', function () {
$discount = 2;
$price = 100;
$total = $price - $discount;
return $total;
});
In example two we have /price as the requested webpage and we returned the total sum, when you visit http://127.0.0.1:8000/price, you will get the output of 98 as the final price.
Note that in route it is good to only have your request which points to the controller where all your codes will lives-in, we will see the controller and views in actions very soon.
Example 3
Do it your self
- provide a get route with the request ur named /test
- inside the route, I will like you to return the following
- your full name
- age
- occupation
Hurray! you have mastered laravel routes, congrats you can now move to the next class here
Dont forget if you have any issues or encountered errors drop it in the comment sections, it will be attended to!
Be first to comment