Thursday, June 2, 2016

Laravel 5.2 Hello World Controller Example

Lets first see what a controller is and what assignment it performs, then I would make a straightforward controller that prints hello world message to program.

Controller is center part in MVC engineering. A controller in Laravel perform three noteworthy undertakings:


  1. Controller decipher the solicitation i.e. separating solicitation and URL course parameters.
  2. Controller associate with application determination state, called Model. Model is not restricted to database; associating with reserves, indexing servers, registry servers, record store, or even outsider APIs is all a player in steadiness state related operations. Controller don't perform these operations itself, it calls other programming module or segments e.g. administrations or information access items to speak with diligence state. 
  3. In view of URL, solicitation and course parameters and results returned by Model, controller renders reaction to client. Controller render the reaction utilizing one of these methodologies: 



  • Controller passes information recovered from model to view format and renders view layout i.e. a PHP document, to client. 

  • Rather than dispatching solicitation to PHP record to render view, Controller can give back the reaction message straightforwardly. This ordinarily happen for AJAX based solicitations or when we utilize controllers to make RESTful administrations. (I would cover how to utilize AJAX and RESTful administrations utilizing Laravel as a part of discrete posts). 

  • Controller may divert program to another URL. For instance, AccountsController can divert program to DashboardController in the wake of making new client account. 


Once the Laravel has recognized a controller utilizing demand URL design utilizing rules characterized as a part of routes.php document, it exchanges the solicitation handling undertaking to Controller. I trust you got the essential thought of what controller does, now I disclose how to make first controller. At that point I characterize mapping for the controller and clarify how aforementioned stuff can be accomplished automatically.

Make EMPTY CONTROLLER :

There is extremely useful summon line device called Artisan that accompanies Laravel, which contains numerous strategies to perform diverse assignments. Rather than making class in out venture, we can utilize Artisan charge to make a controller for us. Indicate your CLI reassure your venture root and issue the accompanying order to make HelloWorldController.

C:\wamp\www\laraveltutorials>php artisan make:controller HelloWorldController
Controller created successfully.

C:\wamp\www\laraveltutorials>

After this charge, you can see HelloWorldController.php class put in app\Http\controller envelope having taking after substance.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class HelloWorldController extends Controller
{
     
}



Characterizing INDEX METHOD IN HELLOWORLDCONTROLLER:



You see there is no technique in the controller above as a matter 
of course. We should characterize techniques inside controller to
really prepare the solicitations. Lets characterize and list 
strategy inside the controller that profits a string straightforwardly
to client program. At that point we should characterize mapping 
in routes.php record to delineate helloworld URL example to file
strategy inside HelloWorldController. Here is the upgrade controller code:



class HelloWorldController extends Controller
{   
    function index(){
        return "HelloWorldController index method called.";       
    }
}


Lets characterize the mapping in routes.php record so that when
clients enters/helloworld in the program, Laravel
dispatch the solicitation handling to HelloWorldController's list technique


Route::get('/helloworld', 'HelloWorldController@index');


Its clear as crystal, we have quite recently characterized decide
that request that Laravel course GET ask for on/helloworld
URL examples to file strategy for HelloWorldController class. Here
is the means by which client would enter URL and what reaction client
would get:





No comments:

Post a Comment