-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathHeroController.php
More file actions
40 lines (34 loc) · 1.1 KB
/
Copy pathHeroController.php
File metadata and controls
40 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace App\Controllers;
use App\Models\HeroModel;
use CodeIgniter\Exceptions\PageNotFoundException;
class HeroController extends BaseController
{
/**
* View a single hero's details.
*
* The $id parameter is the hero's ID, and is
* passed in from the route definition as $1,
* since it is the first placeholder in the route.
*/
public function show(int $id): string
{
// When you only need to use a model in a single place,
// you can simply get a new instance here. It will use
// the default database connection if none is passed in
// during instantiation.
$heroes = new HeroModel();
// Locate a single hero, by ID.
$hero = $heroes->find($id);
// If the hero was not located, throw an exception.
if ($hero === null) {
throw new PageNotFoundException('Hero not found');
}
// Return a view, passing the variables
// you want to access in the view within the
// second parameter.
return view('hero', [
'hero' => $hero,
]);
}
}