How to create controller in Laravel 4.2 - BOOKC -->

How to create controller in Laravel 4.2

Thursday, April 21, 2016


Controller is where you put most of your application logic, you can make an instance of a model inside controller and then display the information to a view, that's the idea of MVC (Model View Controller).

In laravel 4.2 controllers are stored on app/controllers directory, so if you want to create a new controller simply put the file inside that directory.

And then after creating the controller, you need to create the routes that will call the controller, in previous article i already show you how to create routes.

Okay, let's begin with creating a new controller called DummyController.php, i'll show you what a controller looks like.

DummyController.php
class DummyController extends Controller
{
public function dummyMethod()
{
return 'this is just a dummy method';
}

public function dummyMethod2()
{
$data = new stdclass();
$data->name = 'Elizabeth';
$data->age = 27;
$data->gender = 'female';
$data->hobby = array('yoga', 'cooking');

return Response::json($data);
}
}

Next create new routes for the controller and calling the method inside the controller, like this:

routes.php
Route::get('/dummy1', 'DummyController@dummyMethod');

Route::get('/dummy2', 'DummyController@dummyMethod2');

Okay, go ahead try it, open /dummy1 and /dummy2, the first one is just displaying string and the second is outputing data in json format.

Remember when my previous two article, we create migration for table users and roles, and then we create the model for each tables.

Now, let's create a real controller that uses the model that we create before, the controller will be responsible for inserting and displaying data from table users.

UserController.php
class UserController extends Controller
{

public function createUser()
{
$name = Input::get('name');
$gender = Input::get('gender');
$status = Input::get('status');
$password = Input::get('password');

$newUser = new User();
$newUser->name = $name;
$newUser->password = Hash::make($password);
$newUser->gender = $gender;
$newUser->status = $status;

if ($newUser->save()) {
return 'data successfully inserted';
} else {
return 'data failed to insert';
}

}


public function displayUser()
{
$users = User::get();

if (count($users) === 0) {
return 'there is no data to display at the moment';
} else {
return Response::json($users);
}

}
}

routes.php
Route::post('/insert-user', 'UserController@createUser');

Route::get('/list-user', 'UserController@displayUser');

The first API is using http POST method and the second one is http GET method, for GET method is okay you can call directly from web browser, but for the POST method, you need a tool called postman.

Postman is a google chrome extension for testing API, so go ahead install postman if you are using google chrome or similar tool if you are using different browser, here's what it look like.






This tutorial is based on :
http://laravelhowto.blogspot.com/2016/04/how-to-create-database-migration-in-laravel-42.html
http://laravelhowto.blogspot.co.id/2016/04/how-to-create-model-in-laravel-42.html