Learn to Understand Routes on Laravel 5 for Beginners

Seegatesite.com – Routes, Model, View, Controller is an important concept to the laravel framework . This is my third article on tutorial laravel framework. If you need to start learning how to install laravel 5 , please follow my tutorial here. Before we go further to create project using laravel we must understand the true concept of MVC and Routes in advance. I myself while writing this article is still on learning phase for using laravel framework. Let us learn together 🙂

Routes resemble custom permalink at the wordpress. With the route we can write the url name according to our will.

Routes on the laravel set on /app/Http/routes.php . Script routes.php by default as shown below

Routes Default Learn To Understand Routes On Laravel 5 For Beginners

For example if we want to create “about” static pages that can be accessed http://your-domain.com/about or on the local server http://localhost/your-folder/about, add the following script on the routes.php

Then the browser will display a “about” page like the following picture

Create About Page Routes Learn To Understand Routes On Laravel 5 For Beginners

In this article I have eliminated the a public url in laravel 5, so that when accessing the home page , the url be as follows http://localhost/your-domain/ or http://your-domain.com/ . If you not yet eliminate the public url in laravel 5 framework, please access the about page with following url : http://localhost/your-folder/public/about or read my article about Best Way Eliminate Public Folder in the URL Laravel 5

A little explanation of the route script above

– Get parameter show us to send GET request.
– ‘/about’ is a custom URL that we create in order to access the about page
– Function(){…} is a closure that will provide answers to the request that we sent. In addition to the closure we can direct the request to a controller function as Route to call the welcome page url.

In the routes.php default script we see code like the following

Route::get(‘/’, ‘WelcomeController@index’);


– ‘/’ an index page / homepage.
– laravel 5 will run a controller named WelcomeController (in the folder /app/Http/Controllers/WelcomeController) and executes the index method / function contained in WelcomeController.

Usually, a beginner like me when trying to learn routes will experience a 404 error (page not found). The problem occurs because of the possibility of setting httpd.conf or apache2.conf (ubuntu 14). You can try the following solution

How to solve 404 error page not found on routes laravel on ubuntu

  • sudo a2enmod rewrite
  • Edit /etc/apache2/apache2.conf (sudo gedit /etc/apache2/apache2.conf) and change AllowOverride None to AllowOverride All
  • sudo service apache2 restart

As an experiment create a new Route in routes.php with the script as follows


and back to WelcomeController.php and add the following code at the bottom of the index function

Please analysis the results

Learn Routes Parameters Laravel 5 for Dummy

This route parameters function is to send a value or parameter to the route or to the controller. On the route parameters there are some parts :

  1. Basic Route Parameter

    Explanation :

      • profile/{name} , untuk mendifinisikan url , sedangkan {name} adalah parameter yang dikirimkan.
      • function($name) , digunakan untuk menangkap parameter.

    Jalankan url berikut untuk melakukan pengecekan script


    Basic Route Parameters Learn To Understand Routes On Laravel 5 For Beginners

  2. Route Parameters

    To send more than one parameter, we only need to add /{new-params}

    Route Parameters Learn To Understand Routes On Laravel 5 For Beginners

  3. Optional Route Parameters

    An option if the parameter isn’t definition it will be replaced with null value so program will be continue. For example, in the previous example you remove the parameter name, what is going on, the program will be error. Now let us look at an example of this optional route parameters.

    Optional Route Parameters Learn To Understand Routes On Laravel 5 For Beginners

    Explanation :

    {age?} , Use of symbols ? behind the parameter means that if the parameter isn’t found to replace null with the following code $age = null.

    We can replace the null value with a default value, eg, $age = 30

Besides get request, route to the laravel have three basic routes that another such post, put and delete. For example, please copy the following code

Please run on the browser, then by default you’ll get results from a get request. Post, put, delete request,  requires a form to execute it. Try script Route::get as below to run a post request.

Please try on your browser and see the results

Post Request Laravel 5

To run the put request, please add the following script on the Route::get

To run the delete request, please add the following script on the Route::get

Delete Request Laravel 5

How about routes on laravel ? is easy right ? Thus article about Learn to Understand Routes on Laravel 5 for Beginners, hope useful.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.