KEMBAR78
Connection of Controller With View-Part2 | PDF | Software Development | Software
0% found this document useful (0 votes)
20 views9 pages

Connection of Controller With View-Part2

Uploaded by

Mr BuggyMan
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)
20 views9 pages

Connection of Controller With View-Part2

Uploaded by

Mr BuggyMan
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/ 9

Connection of Controller with View: Part-2

Objective: Understand how controllers work and how to route requests to them.

Example 1: Using If-Else in Controller


ExampleController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function checkCondition()
{
$value = 15; // Static value for demonstration

if ($value > 10) {


$message = "The value is greater than 10.";
} else {
$message = "The value is 10 or less.";
}

return view('condition', ['message' => $message]);


}
}
View: resources/views/condition.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Condition Example</title>
</head>
<body>
<h1>{{ $message }}</h1>
</body>
</html>

Route in web.php
use App\Http\Controllers\ExampleController;
Route::get('/condition', [ExampleController::class, 'checkCondition']);
Example 2: Using For Loop in Controller
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function generateNumbers()
{
$numbers = [];

for ($i = 1; $i <= 10; $i++) {


$numbers[] = $i;
}

return view('numbers', ['numbers' => $numbers]);


}
}
}
View: resources/views/numbers.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Numbers Example</title>
</head>
<body>
<h1>Generated Numbers</h1>
<ul>
@foreach ($numbers as $number)
<li>{{ $number }}</li>
@endforeach
</ul>
</body>
</html>
}
Route in web.php

use App\Http\Controllers\ExampleController;

Route::get('/numbers', [ExampleController::class, 'generateNumbers']);


Example 3: Using a Switch in Controller
ExampleController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function chooseColor($color)
{
switch ($color) {
case 'red':
$message = "You chose Red.";
break;
case 'blue':
$message = "You chose Blue.";
break;
case 'green':
$message = "You chose Green.";
break;
default:
$message = "Invalid color.";
}

return view('color', ['message' => $message]);


}
}
View: resources/views/color.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Color Choice</title>
</head>
<body>
<h1>{{ $message }}</h1>
</body>
</html>
Route in web.php
use App\Http\Controllers\ExampleController;

Route::get('/choose-color/{color}', [ExampleController::class, 'chooseColor']);


Example 4: Random Number Generator
ExampleController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function randomNumbers()
{
$numbers = [];

for ($i = 0; $i < 5; $i++) {


$numbers[] = rand(1, 100); // Generate random numbers between 1 and 100
}

return view('random', ['numbers' => $numbers]);


}
}
View: resources/views/random.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Random Numbers</title>
</head>
<body>
<h1>Generated Random Numbers:</h1>
<ul>
@foreach ($numbers as $number)
<li>{{ $number }}</li>
@endforeach
</ul>
</body>
</html>
Route in web.php
use App\Http\Controllers\ExampleController;

Route::get('/random', [ExampleController::class, 'randomNumbers']);


Example 5: Simulate Conditional Rendering
ExampleController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function checkAccess($age)
{
if ($age >= 18) {
return view('adult', ['age' => $age]);
} else {
return view('minor', ['age' => $age]);
}
}
}
View: resources/views/adult.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Access Granted</title>
</head>
<body>
<h1>Welcome, you are {{ $age }} years old. Access granted!</h1>
</body>
</html>
View: resources/views/minor.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Access Denied</title>
</head>
<body>
<h1>Sorry, you are {{ $age }} years old. Access denied!</h1>
</body>
</html>
Route in web.php
use App\Http\Controllers\ExampleController;

Route::get('/check-access/{age}', [ExampleController::class, 'checkAccess']);


Example 6: Simulate an API Call
ExampleController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function simulateApi()
{
$data = [
'status' => 'success',
'results' => [
['name' => 'Alice', 'score' => 85],
['name' => 'Bob', 'score' => 92],
['name' => 'Charlie', 'score' => 78]
]
];

return view('api_simulation', ['data' => $data]);


}
}
View: resources/views/api_simulation.blade.php
<!DOCTYPE html>
<html>
<head>
<title>API Simulation</title>
</head>
<body>
<h1>API Results:</h1>
@if ($data['status'] === 'success')
<ul>
@foreach ($data['results'] as $result)
<li>{{ $result['name'] }}: {{ $result['score'] }}</li>
@endforeach
</ul>
@else
<p>Failed to fetch data.</p>
@endif
</body>
</html>
Route in web.php
use App\Http\Controllers\ExampleController;
Route::get('/api-simulate', [ExampleController::class, 'simulateApi']);

Example 7: Generate a Chessboard Grid

ExampleController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function chessboard()
{
$rows = 8;
$cols = 8;

return view('chessboard', ['rows' => $rows, 'cols' => $cols]);


}
}
View: resources/views/chessboard.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Chessboard</title>
<style>
table { border-collapse: collapse; }
td { width: 50px; height: 50px; text-align: center; }
.black { background-color: black; color: white; }
.white { background-color: white; }
</style>
</head>
<body>
<h1>Chessboard</h1>
<table border="1">
@for ($i = 0; $i < $rows; $i++)
<tr>
@for ($j = 0; $j < $cols; $j++)
@php
$isBlack = ($i + $j) % 2 === 0;
@endphp
<td class="{{ $isBlack ? 'black' : 'white' }}"></td>
@endfor
</tr>
@endfor
</table>
</body>
</html>
Route in web.php
use App\Http\Controllers\ExampleController;

Route::get('/chessboard', [ExampleController::class, 'chessboard']);

Example 8: Dynamic Form with Validation

ExampleController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExampleController extends Controller


{
public function showForm()
{
return view('form');
}

public function validateForm(Request $request)


{
$validated = $request->validate([
'name' => 'required|max:50',
'age' => 'required|integer|min:1|max:100'
]);

return view('form_result', ['data' => $validated]);


}
}
View: resources/views/form.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Form Example</title>
</head>
<body>
<h1>Enter Your Details</h1>
<form action="/submit-form" method="POST">
@csrf
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<br>
<label for="age">Age:</label>
<input type="number" id="age" name="age">
<br>
<button type="submit">Submit</button>
</form>

@if ($errors->any())
<div>
<h3>Errors:</h3>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</body>
</html>
View: resources/views/form_result.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Form Result</title>
</head>
<body>
<h1>Form Submitted Successfully!</h1>
<p>Name: {{ $data['name'] }}</p>
<p>Age: {{ $data['age'] }}</p>
</body>
</html>
Routes in web.php
use App\Http\Controllers\ExampleController;

Route::get('/form', [ExampleController::class, 'showForm']);


Route::post('/submit-form', [ExampleController::class, 'validateForm']);

You might also like