Skip to content

Commit 87cd39b

Browse files
committed
modify how resolve View
1 parent e894a38 commit 87cd39b

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

src/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DB_PASS=mypassword

src/App/Controllers/HomeController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
class HomeController
99
{
10-
public function index():string
10+
public function index():View
1111
{
12-
return (new View('index'))->render();
12+
return View::make('index' , ['foo' => 'test']);
1313
}
1414

1515
public function upload()

src/App/Controllers/InvoiceController.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22

33
namespace App\Controllers;
44

5+
use App\View;
6+
57
class InvoiceController
68
{
7-
public function index(): string
9+
public function index(): View
10+
{
11+
return View::make('invoices/index');
12+
}
13+
public function create(): View
814
{
9-
return 'InvoiceController';
15+
return View::make('invoices/create');
1016
}
11-
public function create(): string
17+
public function store()
1218
{
13-
return 'Create';
19+
$invoice= new Invoice();
20+
$amount= $_POST['amount'];
21+
$invoice->store($amount);
22+
var_dump($amount);
1423
}
1524
}

src/App/View.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,34 @@ public function __construct(
1616
){
1717
}
1818

19+
public static function make(string $view, array $params =[]) : static
20+
{
21+
return new static($view,$params);
22+
}
23+
1924
public function render(): string
2025
{
2126
$filePath=VIEW_PATH.'/'.$this->view.'.php';
2227
if(!file_exists($filePath)){
2328
throw new ViewNotFoundException();
2429
}
2530

31+
// foreach ($this->params as $key=> $value){
32+
// $$key=$value;
33+
// }
34+
35+
extract($this->params);
2636
ob_start();
2737
include $filePath;
2838
return (string) ob_get_clean();
2939
}
40+
41+
public function __toString(): string
42+
{
43+
return $this->render();
44+
}
45+
public function __get(string $name)
46+
{
47+
return $this->params[$name] ?? null;
48+
}
3049
}

src/views/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?php echo $foo?>
12
<form action="/upload" method="post" enctype="multipart/form-data>">
23
<input type="file"name="recipt">
34
<button type="submit">Upload</button>

0 commit comments

Comments
 (0)