1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* This is a Anax frontcontroller. |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
// Get environment & autoloader. |
8
|
|
|
require __DIR__.'/../config.php'; |
9
|
|
|
|
10
|
|
|
// Create services and inject into the app. |
11
|
|
|
$di = new \Anax\DI\CDIFactoryTest(); |
12
|
|
|
$app = new \Anax\Kernel\CAnax($di); |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
$di->set('TestController', function () use ($di) { |
16
|
|
|
$controller = new TestController(); |
17
|
|
|
$controller->setDI($di); |
|
|
|
|
18
|
|
|
return $controller; |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class TestController |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
use \Anax\DI\TInjectable; |
25
|
|
|
|
26
|
|
|
public function indexAction() |
27
|
|
|
{ |
28
|
|
|
$this->theme->setTitle("Delegate view creation to other methods/actions and use forward"); |
|
|
|
|
29
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
30
|
|
|
'title' => "Delegate view creation to other methods and use forward", |
31
|
|
|
'content' => "Add views by using forward. This page is built up by several views, combined in one controller/action but added using dispatcher->forward().", |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
35
|
|
|
'controller' => 'test', |
36
|
|
|
'action' => 'addView1', |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$this->dispatcher->forward([ |
|
|
|
|
40
|
|
|
'controller' => 'test', |
41
|
|
|
'action' => 'addView2', |
42
|
|
|
'params' => ["A_PARAMETER", "ANOTHER PARAM"], |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
46
|
|
|
'title' => "Last view", |
47
|
|
|
'content' => "This view is created in indexAction()", |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function addView1Action() |
52
|
|
|
{ |
53
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
54
|
|
|
'title' => "View 1.1", |
55
|
|
|
'content' => "This view is created in addView1Action()", |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
59
|
|
|
'title' => "View 1.2", |
60
|
|
|
'content' => "This view is created in addView1Action()", |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function addView2Action($str1, $str2) |
65
|
|
|
{ |
66
|
|
|
$this->views->add('default/page', [ |
|
|
|
|
67
|
|
|
'title' => "View 2", |
68
|
|
|
'content' => "This view is created in addView1Action(), the parameter values are: $str1, $str2", |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$app->router->add('', function () use ($app) { |
|
|
|
|
76
|
|
|
|
77
|
|
|
$app->dispatcher->forward(['controller' => 'test']); |
|
|
|
|
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
// Check for matching routes and dispatch to controller/handler of route |
82
|
|
|
$app->router->handle(); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// Render the page |
85
|
|
|
$app->theme->render(); |
|
|
|
|
86
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.