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()');
Program:
php
Route::get( '/sayhello' , function () {
return 'Hey ! Hello' ;
})
|
Output:

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 that 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:
php
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Hello! World.</h1>
</body>
</html>
|
Program 2: Add the following code to your web.php now.
php
Route::get( '/viewhello' , function () {
return view( 'index' );
});
|
Output:

In the code given above, we have used /viewhello as the name of the route and in the callback function we have used a view() method which is an inbuilt 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 with 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 the following code in this file:
Program 1: The code written below is a basic controller code where we are just using the Controllers namespace to add the capability to use it, it’s just like importing the libraries. Let’s add the function now:
php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class mycontroller extends Controller {
}
|
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:
php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class mycontroller extends Controller {
public function index() {
return view( 'index2' );
}
}
|
Program 3: We have written the frontend file, and written controller, and now the last thing is registering the route.
php
<!DOCTYPE html>
<html lang="en">
<body>
<h1>This is index 2.</h1>
</body>
</html>
|
Syntax: For registering the routes
Route::request_type('/url', 'ControllerName@functionName');
Note: Here ControllerName is the name of the 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 the 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.
php
Route::get( '/viewindex2' , 'mycontroller@index' );
|
Output:

Similar Reads
Laravel | Migration Basics
In Laravel, Migration provides a way for easily sharing the schema of the database. It also makes the modification of the schema much easier. It is like creating a schema once and then sharing it many times. It gets very useful when you have multiple tables and columns as it would reduce the work ov
4 min read
Laravel | View Basics
Laravel is an MVC based PHP framework. In MVC architecture, V stands for View. The View is data that is going to be displayed to the user on their browser and the user can interact with it. It is simply an interface provided to the user for interaction. Laravel used a powerful templating engine call
8 min read
Laravel | CSRF Protection
Cross-Site Request Forgery (CSRF) is a type of attack that performed by the attacker to send requests to a system with the help of an authorized user who is trusted by the system. Laravel provides protection with the CSRF attacks by generating a CSRF token. This CSRF token is generated automatically
3 min read
Laravel | Eloquent Model Basics
Laravel is an MVC based PHP framework. In MVC architecture, âMâ stands for âModelâ. A Model is basically a way for querying data to and from the table in the database. Laravel provides a simple way to do that using Eloquent ORM (Object-Relational Mapping). Every table has a Model to interact with th
5 min read
Flask App Routing
App Routing means mapping the URLs to a specific function that will handle the logic for that URL. Modern web frameworks use more meaningful URLs to help users remember the URLs and make navigation simpler. Example: In our application, the URL ("/") is associated with the root URL. So if our site's
3 min read
Laravel | Controller Basics
Laravel is an MVC based PHP framework. In MVC architecture, 'C' stands for 'Controller'. A Controller is that which controls the behavior of a request. It handles the requests coming from the Routes. In Laravel, a controller is in the âapp/Http/Controllersâ directory. All the controllers, that are t
2 min read
Backbone.js js Routing
Backbone.js is a compact library used to organize JavaScript code. Another name for it is an MVC/MV* framework. If you are not familiar with MVC, it's just a method for creating user interfaces. JavaScript functions make it much simpler to create a program's user interface. Models, views, events, ro
3 min read
Laravel Tutorial
Laravel is an open-source PHP web application framework that has gained immense popularity since its inception in 2011, created by Taylor Otwell. This renowned framework empowers developers to build robust, scalable web applications with remarkable ease. As a developer-friendly framework, Laravel of
3 min read
Laravel | Validation Rules
Validating the data coming from the userâs end is very important as it can help the user in filling proper data and prevent them from submitting any improper or malicious request. In Laravel, there are Validation Rules which are predefined rules, which when used in Laravel application. There is a li
5 min read
Laravel vs Spring Boot: Top Differences
Laravel and Spring Boot are the two most popular tools used to develop applications. A comparison between the two can be seen as a scenario where you need to build a house. You can either use all the tools that will help build the house with wood or tools that will help build the house with metal. T
9 min read