Once you have installed Laravel and your basic Web App is up and running. Let’s just look more deeply into the framework and see how we can work with routes.
Routes: Routes are actually the web URLs that you can visit in your web application. For example /home, /profile, /dashboard etc are all different routes that one can create in a Laravel Application. Keep in mind that, routes are case sensitive thus /profile is different than /Profile.
Creating Routes: In Laravel, all of our routes are going to be written in routes/web.php file as this directory is made standard for all our web-related routes. Open this file and let’s create our first route with Laravel, write to the end of this file.
- Syntax: To write route is as given below:
// Syntax of a route Route::request_type('/url', 'function()');
// Creating a new route
'Hey ! Hello'
Breaking down the code given above Route::get means this is a route that will expect a GET request. The /sayhello is the name of the route and you can create a route with any name. Further, we have to specify what to do when we visit that route in the browser and we do so in the form of a callback function which returns a string saying Hey ! Hello.
Returning Web-page: Instead of just returning strings, we are going to return webpages when someone visits a route. Let’s see how we can do that. First of all create a file called index.blade.php in resources/views. In Laravel we have a built-in templating engine called Blade thus we write all of our webpages in *.blade.php not *.html.
- Program 1:
- Program 2: Add following code to your web.php now.
// Creating a new route
The code given above we have used /viewhello as the name of route and in the callback function we have used a view() method which is a inbuild method provided by Laravel to serve webpage and it automatically picks the file matching from resources/views folder. For example, passing ‘index’ will serve index.blade.php.
Routes with controllers: Laravel provides us much more power than just writing a direct callback function. We can actually make our routes point to a function inside the controllers. To do so let’s manually create our controller first and call it mycontroller. Just go to app/Http/Controllers and create a file called mycontroller.php. Write following code in this file:
- Program 1: Code written below is a basic controller code where we are just using Controllers namespace to add the capability to use it, it’s just like importing the libraries. Let’s add the function now:
// All the functions written here
// can be used in routes
- Program 2: Here we have created a function called index() and inside it we are using view method to serve index2.blade.php. Now let’s make such file in resources/views and add the following code to it:
- Program 3: We have written frontend file, written controller and now the last thing is registering the route.
<h1>This is index 2.</h1>
- Syntax: For registering the routes
Note: Here ControllerName is the name of controller and functionName is the name of the function to be used when a user visits that URL. Let’s follow this syntax and write our route in routes/web.php at the end of file:
- Program 4: Here you can see that I have written mycontroller as my controller and index as the name of the function to be attached to this url. Now let’s visit the /viewindex2 and see the output.
- Laravel | Migration Basics
- Laravel | View Basics
- Laravel | Controller Basics
- Laravel | Eloquent Model Basics
- Laravel | Artisan Commands to know in Laravel
- Differences Between Django vs Laravel
- Laravel | Front-end Scaffolding
- Laravel | Delete Records
- Laravel | Installation and Configuration
- Laravel | Directory Structure
- Laravel | CSRF Protection
- Laravel | Validation Rules
- What are the differences and Similarities Between Lumen and Laravel?
- Laravel | Artisan Console Introduction
- Laravel | Blade Templates Inheritance
- Difference Between Laravel and Ruby on Rails
- Difference Between Laravel and CodeIgniter Framework in PHP
- Laravel | MySQL Database Connection
- Different ways for passing data to view in Laravel
- Which One is Most Demanding Back-End Web Framework between Laravel , NodeJS and Django ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.