Profile Picture
Written ByMd Tanveer

PHP framework Laravel Route

Laravel is an PHP framework. It’s one of the most popular framework among a lot of PHP frameworks. Route is the first thing you will come across when you will start learning Laravel. Here I will try to cover some basis of Route like: Route’s structure, Multiple Route, Passing Parameter(s) and Naming Route.

Aug 4, 2019 @ 11:37 PM

Route’s structure

Route is a class in Laravel where you have “get”, “post”, “put”, “patch” and “delete” static methods. You need to pass 2 parameters into the method. The first parameter you provide is the path/route of your file which you want to load. If you type that path into the URL of your browser you will see the page you intended to see. The second parameter could be anything. In the below picture it’s a closure (call back function). The job of this closure is to do something when you access the URL. In the below picture the closure is returning a welcome page. This is the first page you see when you first install Laravel.

‘/’ means the root directory. For this site it will be
. When you pass the URL as 1st parameter Laravel adds the root path so you do not have to add it explicitly. You just need to provide what comes after domain name as domain name is common for every route.

Now if you change the first parameter from ‘/’ (Root directory) to ‘/show’ (/show) and access that using your browser you will see the same page. This is because you are telling to display the “welcome” page if someone accesses ‘/show’ URL. Now if you change the return content from “return view(‘welcome')” to “return ‘Hello world’” and then access the “/show” you will see ‘Hello world’ since you are now not returning any view rather returning the string ‘Hello World’.

Creating multiple Routes

It’s simple. Just add another route with same 2 numbers of parameter. Change the path to locate your file in view. In the below picture I am just returning a string “This is about page”. And if you type

you will see “This is about page” in your browser.

Remember, for me the domain name is
. For you it should or could be different.

Passing parameter(s) through route

A lot of time you need to pass parameter(s) through your URL. For example if you have bunch of items in your database and you would like to show the 1st item then you need to pass the id of that item. In the below example we will be passing the id for items.

In the path put the parameter inside curly braces. Then in the closure pass the parameter to access it inside the closure. You can pass multiple parameter separated by forward slash ‘/’ in the path and separated by coma ‘,’ in the closure. Now if you put
in your browser URL you will see this result: “The id passed through URL is 1”

Here we are passing 2 parameter. 1st one is id and 2nd one is title.The output would be : “The id and title passed through URL is 1 Laravel”

Naming Route

If you find the path is too long in a route you can give it a short name. This is useful to put an a long URL in an anchor tag.

In the picture you can see we are making the path “item/show/list” shorter to “item.list”. Now instead of putting “item/show/list” this in an anchor tag you can put “route(item.list)”
. If you access this URL “/item/show/list” now you will see the whole URL. For me it looks like this:
php artisan route:list

Now you can go to the folder where you have your Laravel installed from terminal and type the above command. You will see the below list of all routes available for you where you can see your recent named route as well.