File tree Expand file tree Collapse file tree 7 files changed +106
-1
lines changed
Expand file tree Collapse file tree 7 files changed +106
-1
lines changed Original file line number Diff line number Diff line change 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 * --------------------------------------------------------------------
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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 () ?>
You can’t perform that action at this time.
0 commit comments