0% found this document useful (0 votes)
15 views6 pages

Kevin Adrianus Johannes - 1204202131 - Modul 4

The document contains PHP code for several Laravel controllers and their corresponding views. The HelloWorld controller contains methods to return greeting messages with names. The Hitung controller contains methods to calculate arithmetic operations from user input on a hitung view, and return results on a hasil view. The FormNameHandling controller contains a method to handle form submission of a name from a form view, and return the name on a success_name view. Routes are defined in Web.php to link the controller methods and views.

Uploaded by

kevin adrianus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Kevin Adrianus Johannes - 1204202131 - Modul 4

The document contains PHP code for several Laravel controllers and their corresponding views. The HelloWorld controller contains methods to return greeting messages with names. The Hitung controller contains methods to calculate arithmetic operations from user input on a hitung view, and return results on a hasil view. The FormNameHandling controller contains a method to handle form submission of a name from a form view, and return the name on a success_name view. Routes are defined in Web.php to link the controller methods and views.

Uploaded by

kevin adrianus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Controller :

HelloWorld.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HelloWorld extends Controller
{
//
// public function index () {
// return "HelloWorld, teks ini berasal dari Controller
HelloWorld";
//}
// public function getName(){
// $name = 'Berlian';
// return ($name);
//}
//public function index(){
// $nama = $this -> getName();
//return "Hello $nama, teks ini berasal dari Controller Hello
World";
// }

public function getName () {


$name = 'Berlian';
return ($name) ;
}

public function getFullName($first, $last) {


$name = $first.''.$last;
return ($name);
}

public function index () {


$nama = $this -> getName();
$last_name = 'lidiawaty';
$full_name = $this -> getFullName($nama, $last_name);
return "Hello $full_name, teks ini berasal dari controller
HelloWorld";
}

public function hello($nama){


return "Hello $nama, data $nama didapat dari routing";
}

}
Hitung.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Hitung extends Controller


{
public function hitung()
{
return view('hitung');
}
public function proses(Request $request)
{
$bilangan1 = $request->input('bilangan1');
$bilangan2 = $request->input('bilangan2');
$hasil_penjumlahan = $bilangan1 + $bilangan2;
$hasil_pengurangan = $bilangan1 - $bilangan2;
$hasil_perkalian = $bilangan1 * $bilangan2;
$hasil_pembagian = $bilangan1 / $bilangan2;
return view('hasil', [
'bilangan1' => $bilangan1,
'bilangan2' => $bilangan2,
'hasil_penjumlahan' => $hasil_penjumlahan,
'hasil_pengurangan' => $hasil_pengurangan,
'hasil_perkalian' => $hasil_perkalian,
'hasil_pembagian' => $hasil_pembagian
]);
}
}

FormNameHandling.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FormNameHandling extends Controller


{

public function handleForm(Request $request){


$nama_input = $request->input('first_name');
return view('success_name', ['nama'=> $nama_input]);
}
}
View :
Form.blade.php

<!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.0">
</head>
<body>
<form method="POST" action="{{route('submit_name')}}">
@csrf
<div class="row row-space">
<div class="col-2">
<div class="input-group">
<label class="label"> fisrt name </label>
<input class="input--style-4" type="text"
name="first_name" placeholder="your name">
</div>
</div>
</div>
<div class="p-t-15">
<button class="btn btn--radius-2 btn--blue"
type="submit">Submit</button>
</div>
</body>
</html>

Success_name.blade.php

<!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.0">
</head>

<body>
<div class="card-body">
<h2 class="tittle">
Hello, {{$nama}} !
</h2>
</div>
</body>
</html>
Hasil.blade.php

@extends('layout')
@section('content')
<h1>Hasil</h1>
<p>Bilangan 1: {{ $bilangan1 }}</p>
<p>Bilangan 2: {{ $bilangan2 }}</p>
<p>Penjumlahan: {{ $hasil_penjumlahan }}</p>
<p>Pengurangan: {{ $hasil_pengurangan }}</p>
<p>Perkalian: {{ $hasil_perkalian }}</p>
<p>Pembagian: {{ $hasil_pembagian }}</p>
<a href="{{ route('hitung') }}">Kembali ke halaman hitung</a>
@endsection

Hitung.blade.php

@extends('layout')
@section('content')
<h1>Hitung</h1>
<form method="POST" action="{{ route('proses') }}">
@csrf
<div>
<label for="bilangan1">Bilangan 1</label>
<input type="number" id="bilangan1" name="bilangan1" required>
</div>
<div>
<label for="bilangan2">Bilangan 2</label>
<input type="number" id="bilangan2" name="bilangan2" required>
</div>
<button type="submit">Hitung</button>
</form>
@endsection

Layout.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Kalkulator</title>
</head>
<body>
<div class="container">
@yield('content')
</div>
</body>
</html>
Web.php

<?php

use App\Http\Controllers\HelloWorld;
use App\Http\Controllers\FormNameHandling;
use App\Http\Controllers\Hitung;

Route::get('/helloworld',[HelloWorld::class,'index']);
Route::get('/hello/{nama}',[HelloWorld::class,'hello']);
Route::post('/namesubmission', [FormNameHandling::class, 'handleForm'])-
>name('submit_name');
Route::get('/hitung', [Hitung::class, 'hitung'])->name('hitung');
Route::post('/hitung', [Hitung::class, 'proses'])->name('proses');

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

Output :

You might also like