Laravel 5.2 Hello World Controller Example
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:
After this command, you can see HelloWorldController.php class placed in app\Http\controller folder having following contents.
Lets define the mapping in routes.php file so that when users enters /helloworld in the browser, Laravel dispatch the request processing to HelloWorldController's index method.
Its self explanatory, we have just defined rule that ask Laravel to route GET request on /helloworld URL patterns to index method of HelloWorldController class. Here is how user would enter URL and what response user would get:
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 and renders view template i.e. a PHP file, to user.
- Instead of dispatching request to PHP file to render view, Controller can return the response text directly. This normally happen for AJAX based requests or when we use controllers to make RESTful services. (I would cover how to use AJAX and RESTful services using Laravel in separate posts).
- Controller may redirect browser to another URL. For example, AccountsController can redirect browser to DashboardController after creating new user account.
Once the Laravel has identified a controller using request URL pattern using rules defined in routes.php file, it transfers the request processing task to Controller. I hope you got the basic idea of what controller does, now I explain how to create first controller. Then I define mapping for the controller and explain how above mentioned stuff can be achieved programmatically.
Create Empty Controller
There is very helpful command line tool called Artisan that comes with Laravel, which contains many methods to perform different tasks. Instead of creating class in out project, we can use Artisan command to create a controller for us. Point your CLI console to your project root and issue the following command to create HelloWorldController
C:\wamp\www\laraveltutorials>php artisan make:controller HelloWorldController Controller created successfully. C:\wamp\www\laraveltutorials>
After this command, you can see HelloWorldController.php class placed in app\Http\controller folder having following contents.
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class HelloWorldController extends Controller { }
Defining index Method in HelloWorldController
You see there is no method in the controller above by default. We must define methods inside controller to actually process the requests. Lets define and index method inside the controller that returns a string directly to user browser. Then we must define mapping in routes.php file to map /helloworld URL pattern to index method inside HelloWorldController. Here is the update controller code:class HelloWorldController extends Controller { function index(){ return "HelloWorldController index method called."; } }
Lets define the mapping in routes.php file so that when users enters /helloworld in the browser, Laravel dispatch the request processing to HelloWorldController's index method.
Route::get('/helloworld', 'HelloWorldController@index');
Its self explanatory, we have just defined rule that ask Laravel to route GET request on /helloworld URL patterns to index method of HelloWorldController class. Here is how user would enter URL and what response user would get:
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
ReplyDeletePHP Institutes in Chennai|PHP Training Center in Chennai
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
ReplyDeleteHadoop-Big-Data-Administration
Thanks for sharing this informative content that guided me to know the details about the training offered in different technology.
ReplyDeletemsbi training in chennai
useful sites and I like the sites..........
ReplyDeleteunix training in chennai
Thanks for sharing .....
ReplyDeletemicrostrategy training in chennai
This comment has been removed by the author.
ReplyDeleteAfter reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
ReplyDeleteClick here:
Microsoft azure training in annanagar
Click here:
Microsoft azure training in velarchery