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
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
1 2 3 4 5 6 | Route::get('/about',function(){ return ' <h1>About</h1> This is about page.thanks for visit '; }); |
Then the browser will display a “about” page like the following picture
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’);
Explanation:
– ‘/’ 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
Table of Contents
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
Route::Get('/profile','WelcomeController@profile');
and back to WelcomeController.php and add the following code at the bottom of the index function
1 2 3 | function profile(){ return 'This is profile page'; } |
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 :
Basic Route Parameter
123Route::get('profile/{name}', function ($name){return 'My name is : '.$name;});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
http://localhost/your-folder/profile/sigit
Route Parameters
To send more than one parameter, we only need to add /{new-params}
123Route::get('/profile/{name}/{age}', function ($name,$age){return 'My name is : '.$name.' , im '.$age.' years old';});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.
123Route::get('/profile/{name}/{age?}', function ($name,$age=null){return 'My name is : '.$name.' , im '.$age.' years old';});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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Route::post('/example',function(){ return 'post example'; }); Route::get('/example', function(){ return 'get example'; }); Route::put('/example', function(){ return 'put example'; }); Route::delete('/example', function(){ return 'delete example'; }); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 | Route::get('/example', function(){ return ' <form action="example" method="POST"> <input type="submit" value="submit" /> <input name="_token" type="hidden" value="'.csrf_token().'" /> </form> '; }); |
Please try on your browser and see the results
To run the put request, please add the following script on the Route::get
1 2 3 4 5 6 7 8 9 10 11 12 | Route::get('/example', function(){ return ' <form action="example" method="POST"><input type="submit" value="submit" /> <input name="_token" type="hidden" value="'.csrf_token().'" /> <input name="_method" type="hidden" value="PUT" /></form> '; }); |
To run the delete request, please add the following script on the Route::get
1 2 3 4 5 6 7 8 9 10 11 | Route::get('/example', function(){ return ' <form action="example" method="POST"><input type="submit" value="submit" /> <input name="_token" type="hidden" value="'.csrf_token().'" /> <input name="_method" type="hidden" value="DELETE" /></form> '; }); |
How about routes on laravel ? is easy right ? Thus article about Learn to Understand Routes on Laravel 5 for Beginners, hope useful.
Leave a Reply