Connect with us

Mastering laravel Routes

kizinho

Published

on

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

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 Integrate Flutterwave Rave Payment System in your Laravel Applications

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to Integrate Flutterwave Rave Payment System in your Laravel Applications [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi Folks, I know many laravel developers that have been using Paystack as their payment system have been finding it difficult to integrate Paystack Laravel Package from Laravel 6.0 - 7.0 versions. Today I have a beautiful solution by using Flutterwave Rave Payment System, this process will work on any laravel versions.RequirementsRunning Exiting Project on LaravelCreate Account on FlutterwaveThen you are good to goLets Begincomposer require kingflamez/laravelrave On...
    Continue Reading

    Laravel Absolute Beginners Masterclass

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Laravel Absolute Beginners Masterclass [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi, folks you are welcome to laravel absolute beginner masterclass, if you are here to learn how laravel works, I will recommend you to visit laravel website here but if you are here to use laravel build a product, you are very welcome. Laravel is a PHP FrameworkWhat is a Framework?Before I dive into explaining Laravel, you will need to understand what is a framework. In general, a Framework is a set of conceptual structure and guidelines, used to build a useful pro...
    Continue Reading

    How to show a nice message to the user when an error occurs in using Guzzle with Laravel

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to show a nice message to the user when an error occurs in using Guzzle with Laravel [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl
    Hi Folks, have you encountered an ugly Exception when making a post, get, put, delete to external API when an error like 500 or 404 occurs. Today you are at the right place, after reading this you will be able to show a nice message to your users in case of any of those errors occursWhen making a request to external API using guzzle to solve this problem you will need to catch the Exception by following the steps below.Prepare your request using Guzzle, I believe yo...
    Continue Reading

    Latest