Connect with us

Mastering laravel Routes

kizinho

Published

on

Follow
DEVELOPER PROGRAMMING: Mastering laravel Routes [New  Developer Programming] » Naijacrawl
Photo: Naijacrawl

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

  1. get - you use get when you want to get resources from the database
  2. post - you use post when you want to create resources for the database
  3. put - you use put when you want to update a specific resource from the database
  4. delete - when you want to delete a specific resource from the database
  5. 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

  1. provide a get route with the request ur named /test
  2. 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!

Click Here To Comment


site


kizinho

Adike Kizito is a top-rated software developer, blogger, sports, and entertainment lover. He is the CEO of these popular websites Naijacrawl.com and Mp3ager.com. He operates his software developing task through Kizzsoft.com,... Read More

Continue Reading
Click to comment

Be first to comment


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    How to fix Flutterwave common error's while Integrating payment in your Laravel App

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to fix Flutterwave common error's while Integrating payment in your Laravel App [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi folks, I was integrating FLutterwave on my laravel app, on the process i found a bunch of errors while trying to use it accept payment on my site. Have been a Paystack heavy user but decided to use Flutterwave for a reason that I love to do. Going through this article I will list the two types of error or bug that you must encounter while using Laravel Rave steps to get Flutterwave payment working.Types of errorError on the payment page when user try to refresh t...
    Continue Reading

    Easy way to Host your Laravel Application on Shared Hosting

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Easy way to Host your Laravel Application on Shared Hosting [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi , Folks have you finished building that your awesome project but hosting it o shared hosting is what remains, don't worry you are on the right track, I have the best way to do that.StepsCreate Database on your online cpanelUpload your project or use git to do thatSet everything up on your ENVnow we need .htaccess that you will create inside your laravel root folderInside the .htaccess add this code RewriteEngine On #ends RewriteCond %{HTTP_HOST} ^(www.)?mydomain...
    Continue Reading

    Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js)

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js) [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js)This article was written by Okonkwo Buchi Flair a ccolleague of mind, read and know how to simply upload your files via Amazon S3.original article can be found hereBefore I begin I would love to point out that is my first post so please pardon me any errors you may find as you read alongRecently I’ve been working on a e-learning platform which of course was going to deal with a...
    Continue Reading

    Latest


    Download Naijacrawl App today

    Fastest way to read on the go

    Download the Naijacrawl App.