Skip to content

Commit 64f43f9

Browse files
committed
feat: View a single hero
1 parent e2c8ead commit 64f43f9

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

app/Config/Routes.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// where controller filters or CSRF protection are bypassed.
2626
// If you don't want to define all routes, please use the Auto Routing (Improved).
2727
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
28-
$routes->setAutoRoute(true);
28+
$routes->setAutoRoute(false);
2929

3030
/*
3131
* --------------------------------------------------------------------
@@ -37,6 +37,9 @@
3737
// route since we don't have to scan directories.
3838
$routes->get('/', 'Home::index');
3939

40+
$routes->get('heroes/(:num)', 'HeroController::show/$1');
41+
$routes->get('dungeons/(:num)', 'DungeonController::show/$1');
42+
4043
/*
4144
* --------------------------------------------------------------------
4245
* Additional Routing
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Controllers\BaseController;
6+
7+
class DungeonController extends BaseController
8+
{
9+
public function index()
10+
{
11+
//
12+
}
13+
}

app/Controllers/HeroController.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Controllers\BaseController;
6+
use App\Models\HeroModel;
7+
use CodeIgniter\Exceptions\PageNotFoundException;
8+
9+
class HeroController extends BaseController
10+
{
11+
/**
12+
* View a single hero's details.
13+
*
14+
* The $id parameter is the hero's ID, and is
15+
* passed in from the route definition as $1,
16+
* since it is the first placeholder in the route.
17+
*/
18+
public function show(int $id)
19+
{
20+
// When you only need to use a model in a single place,
21+
// you can simply get a new instance here. It will use
22+
// the default database connection if none is passed in
23+
// during instantiation.
24+
$heroes = new HeroModel();
25+
26+
// Locate a single hero, by ID.
27+
$hero = $heroes->find($id);
28+
29+
// If the hero was not located, throw an exception.
30+
if ($hero === null) {
31+
throw new PageNotFoundException('Hero not found');
32+
}
33+
34+
// Display a view file, passing the variables
35+
// you want to access in the view within the
36+
// second parameter.
37+
echo view('hero', [
38+
'hero' => $hero,
39+
]);
40+
}
41+
}

app/Views/hero.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!--
2+
We are using View Layouts to provide a consistent
3+
look to the site. The `extend()` method tells the
4+
name of the view that this view will work with to
5+
display the final content. In this case, the view
6+
is at app/Views/layout.php
7+
-->
8+
<?= $this->extend('layout') ?>
9+
10+
<!--
11+
Everything between `section()` and `endSection()
12+
is inserted into the `layout` view where it calls
13+
`$this->renderSection('content')`.
14+
-->
15+
<?= $this->section('content') ?>
16+
<div class="">
17+
18+
<!-- While we should throw exception if the hero is not found,
19+
we'll add a check just in case. -->
20+
<?php if (empty($hero)) : ?>
21+
<div class="alert alert-warning">
22+
Hero not found.
23+
</div>
24+
<?php endif ?>
25+
26+
<!-- Hero Details -->
27+
<?php if ($hero) : ?>
28+
<div class="container text-center">
29+
<div class="col-6 mx-auto">
30+
31+
<div class="card shadow">
32+
<img src="<?= base_url('images/'. esc($hero->image, 'attr')) ?>"
33+
alt="<?= esc($hero->name, 'attr') ?>"
34+
class="card-img-top"
35+
>
36+
<div class="card-body">
37+
<h5 class="card-title"><?= esc($hero->name) ?></h5>
38+
39+
<p class="card-text">Level <?= $hero->level ?? 0 ?> <?= $hero->class ?? '' ?></p>
40+
</div>
41+
</div>
42+
43+
</div>
44+
</div>
45+
46+
<?php endif ?>
47+
</div>
48+
<?= $this->endSection() ?>

public/images/Default.png

1.92 MB
Loading

public/images/pirate.png

1.16 MB
Loading

public/images/ursula.png

1.52 MB
Loading

0 commit comments

Comments
 (0)