How to add and use a Controller in Laravel?

We have started out by handling requests in route files. This requires creating one request method per request type.

Using controllers we can group related requests and provide a single handling system.

This post leads us through the process of creating a simple controller.

Controllers are to be stored in the

app/Http/Controllers directory.

Here is a very simple controller called TestController


<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class TestController extends Controller {

public function show()
{
return "Test Controller";
}

}
?>

The controller has  a show method.

Next we need to define a route in the web.php file inside the routes directory.


Route::get('/test', 'TestController@show');

The path should be self explanatory. show method inside the TestController class.

Go to this URL in your browser.

http://localhost/laraveltest/public/test

and view the results.

 

Let us add some extra functionality to the Controller now.

 

Let us develop  the controller so that it receives a number from the url. Checks  if the number is even or odd and returns an output accordingly.


class TestController extends Controller {

public function show($n)
{
if($n % 2==0)
return "Even";
else
return "Odd";
}

}

The route in web.php


Route::get('/test/{n}', 'TestController@show');

Try calling it.

Finally let us develop a controller to get data from a html form and process it.

 

OddEven Controller


<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class OddEvenController extends Controller {

public function __invoke(Request $request)
{
$n=$request->n;
if($n % 2==0)
return view("oddeven",['result'=>'Even']);
else
return view("oddeven",['result'=>'Odd']);
}

}
?>

We will need to declare 2 routes in the web.php file. One get method for opening the view the first time. Two post method for handling the posted form data.


&nbsp;

Route::get('/oddeven', function () {
return view('oddeven');
});

Route::post('/oddeven', 'OddEvenController');

&nbsp;

 

Here is the view oddevenblade.php


<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Odd Even</title>
{{ Html::style('css/w3.css') }}
</head>
<body class="w3-padding">
<div class="w3-teal w3-card">
<h1>Check for Odd/Even</h1>
{{ Form::open(array('url' => 'oddeven')) }}
<br/>
{{Form::label('n', 'Enter N')}}
<br/>
{{Form::text('n','',array('class' => 'w3-card','title'=>'Enter N','placeholder'=>'Enter N'))}}
<br/>
{{Form::label('result', $result)}}

{{Form::submit('submit')}}






{{ Form::close() }}
</div>
</body>
</html>

 

The generated code of the view


&nbsp;

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Odd Even</title>
<link media="all" type="text/css" rel="stylesheet" href="http://localhost/laraveltest/public/css/w3.css">

</head>
<body class="w3-padding">
<div class="w3-teal w3-card">
<h1>Check for Odd/Even</h1>
<form method="POST" action="http://localhost/laraveltest/public/oddeven" accept-charset="UTF-8"><input name="_token" type="hidden" value="S2vfNNFOsR5ZixNMz50kMMmNMHGovxCjJwF4nrFr">
<br/>
<label for="n">Enter N</label>
<br/>
<input class="w3-card" title="Enter N" placeholder="Enter N" name="n" type="text" value="45" id="n">
<br/>
<label for="result"></label>

<input type="submit" value="submit">






</form>
</div>
</body>
</html>

Leave a Reply