Posts

Showing posts from April, 2016

How to Get Request Data in Laravel 5.2

Image
This post explain how to get request data in routes.php file and in Laravel 5.2 controllers. The request data includes the parameters submitted via URL query string e.g. /search?category=furniture, here category  is parameter name and furniture is parameter value. If a form is submitted via GET or POST, the data that is sent to server is also called request data. In short, request data is data that a request carries. When Laravel framework receive an HTTP request, before transferring request processing to controllers, the framework extract data from request and instantiate an object of type Request , which is Laravel provided class. The Request object contain all the data that HTTP request carries. The Laravel framework passes the object using dependency injection into controller methods. It is passed only if we have expressed our interest by declaring an argument of type Request in controller methods. After receiving the Request object as argument, we can read the parameter valu

How to Get Route Parameters in Controller of Laravel 5.2

Image
Requests in Laravel can contain data in two different ways i.e. as URL route parameters or as request parameters. This article explains how to get URL route parameters inside controller. The Route Parameters are those which are submitted as part of the URL Pattern. Assume users access the url /users/search/lahore.   Here we can treat "lahore" as route parameter for city. Inside Controller we must get city name in a variable so that we can query database for users living in Lahore. To get route parameters, we define appropriate URL pattern in routes.php file. Lets see the mapping for above URL in routes.php file and how we would receive the variable inside controller. Route::get('/users/search/{city}', 'UsersController@search'); We can receive the value of city placeholder inside controller by declaring just an argument in search method, as below: namespace App\Http\Controllers; class UsersController extends Controller { public function search($c

How to Return View from Controller in Laravel 5.2

Image
In previous tutorial , I explained the different operations that a controller performs in Laravel. We also learned how to make hello world controller that return some text to user. In this post, I would explain how to make controllers that return view template. I would also explain how we can pass data to view templates from the controller. In HelloWorldController, we looked at how controller can return short string to browser. In real applications, controller render response using views which are defined outside the controllers. These views are located at /resources/views folder of your Laravel application. After a controller has done its working e.g. interaction with model etc. Controller select a view and send its contents to browser. Remember, views are just PHP files that contains PHP, HTML, CSS etc. To demonstrate above concepts, below I create UsersController. C:\wamp\www\laraveltutorials>php artisan make:controller UsersController Controller created successfully. H

Laravel 5.2 Hello World Controller Example

Image
Lets first see what a controller is and what task it performs, then I would make a simple controller that prints hello world message to browser. Controller is core component in MVC architecture. A controller in Laravel perform three major tasks: Controller interpret the request i.e. extracting request and URL route parameters Controller interact with application persistence state, called Model. Model is not limited to database; interacting with caches, indexing servers, directory servers, file store, or even third party APIs is all part of persistence state related operations. Controller do not perform these operations itself, it calls other software module or components e.g. services or data access objects to communicate with persistence state. Based on URL, request & route parameters and results returned by Model, controller renders response to user. Controller render the response using one of these approaches: Controller passes data retrieved from model to view template

Laravel Routing / Routes.php Tutorial with Examples

Image
All MVC Frameworks provide some mechanism to define rules that are used to map different requests to corresponding controllers. In Laravel MVC Framework, we define these rules in routes.php (located in http\Http folder) file to map different URL patterns to controllers. I assume you have created your first laravel project. Here I explain how to define mapping rules in routes.php file. Simplest Routing Rule Route::get('/hello-world', function () { return "Hello World"; }); Laravel provide a class with different static methods e.g. get, post etc. The method name tells about the type of HTTP request (get, post, ...) for which the rule is defined. The first parameter is URL Pattern, means what a user enter in the browser address bar after the context path or domain name. Here "/hello-world" pattern shows this rule would be used to process or delegate requests coming from domain-name/hello-world pattern. The 2nd parameter can be a string