As discussed in last tutorial, requests are mapped by the routes directory. This directory contains four files:
- web.php - routes for traditional browser HTTP requests
- api.php - routes for API requests (XHR requests)
- console.php - Closure based console commands
- channels.php - Socket channel events
In this tutorial we will deal with web.php (in the file routes >> web.php). By default web.php contains the following code:
- <?php
- /*
- |--------------------------------------------------------------------------
- | Application Routes
- |--------------------------------------------------------------------------
- |
- | Here is where you can register all of the routes for an application.
- | It's a breeze. Simply tell Laravel the URIs it should respond to
- | and give it the controller to call when that URI is requested.
- |
- */
- use Illuminate\Support\Facades\Route;
-
- Route::get('/', 'WelcomeController@index');
- Route::get('home', 'HomeController@index');
Route class has get function which handles GET type requests. It takes two parameters: an url and a closure. In line 14, the url is '/' (means domain name, i.e. www.your-domain.com in production or localhost:8000 on local server). WelcomeController@index is pointing to index() function inside app >> Http >> Controllers >> WelcomeController. It means index() function will execute when we send request for '/'. Similarly when we request for 'localhost:8000/home' then index() function of class HomeController will be executed.
First response:
Now add this code in the end of the file:
- Route::get('request',function(){
- return 'this is my first response';
- });
Now start server and open link localhost:8000/request. The function (second argument) will execute and will return string which will be displayed on the webpage.
Other routing verbs:
Just like get(), Route class also has functions for other request types:
- post('url', 'closure') : It is used for post type requests (like for submitting forms).
- put('url', 'closure') : It is used for inserting data in database tables.
- delete('url', 'closure'): It is used for deleting something from database.
- match('['get', 'post']','url', 'closure'): This function accepts more than one type of requests. In this case, first argument is array of request types which are to be handled.
- any('url', 'closure'): This function accepts all types of requests.
NOTE:
- post(), put() and delete() function will check csrf_token. For using these types, your must add _token in your form.
- <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
- HTML forms do not support put and delete methods. So you will have to add one more hidden field named _method.
- <form action="/action" method="POST">
- <input type="hidden" name="_method" value="PUT">
- <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
- </form>
Controller:
In real world, we have to do some operations and after it we have to render HTML content (or may be some other content) which is usually large in size. So, operations are separately written in the controllers (just like in WelcomeController and HomeController) and content is stored at different place. Now modify our above code like this:
- Route::get('request','WelcomeController@my_request');
Now open app >> Http >> HomeController.php and add this function in the class HomeController.
- <?php
-
- public function my_request()
- {
- $string = 'this is response from controller';
- return $string;
- }
Comment this line in constructor: $this->middleware('guest'); because this line is using a database table. We will use it after reading about databases in Laravel.
Now open url localhost:8000/request again. You will get $string.
Create a Controller:
The controller is generated by using command:
- php artisan make:controller YourController
This command will generate a file with name YourController.php. There is a class inside this file with name YourController which contains common functions: index, create, store, show, edit, delete, update and distroy.
Route parameters:
Sometimes we need dynamic urls. For example, we want to pass user id in url. We can capture these ids from url and can pass it to function as an argument.
- <?php
-
- // change routes.php to like this
- Route::get('request/{id}','YourController@index');
- // change your function in class YourController
- // add parameter to function
- public function index($id)
- {
- $string = "this is response from controller and your id is $id";
- return $string;
- }
Now try these urls:
localhost:8000/request/45412
localhost:8000/request/43422
localhost:8000/request/28411
NOTE:
- Url is captured by writing {parameter_name}. The name inside the braces {} and the parameter name must be same.
- For default parameters use a question mark after parameter_name
- // in routes.php
- // change routes.php to like this
- Route::get('request/{id?}','YourController@index');
- //in YourController.php
- // add default parameter to function
- public function index($id = 111111)
- {
- $string = "this is response from controller and your id is $id";
- return $string;
- }
Rendering views:
Views are the templates which are rendered to the requests. These are placed in the directory resources >> views. For example, index() function in WelcomeController.php is rendering welcome.blade.php file using view() function.
Note that:
- only name of the file is passed to the view() function without any extension.
- view() function will work only with .blade.php and .php files. .blade.php files are laravel template files and you can also write simple html/ php inside these files. In other words, you can assume these files as .php files. In additional, these files supports template features (discussed in next tutorial).
- public function index()
- {
- return view('welcome');
- }