33namespace tebe \zack \routing ;
44
55use Symfony \Component \DependencyInjection \ContainerBuilder ;
6+ use Symfony \Component \HttpFoundation \RedirectResponse ;
67use Symfony \Component \HttpFoundation \Request ;
78use Symfony \Component \HttpFoundation \Response ;
89
10+ use function tebe \zack \html_extract_title ;
11+
912class PhpRouteHandler
1013{
1114 private ?ContainerBuilder $ container ;
@@ -28,11 +31,13 @@ public function __invoke(Request $request): Response
2831 $ outputValue = ob_get_clean ();
2932
3033 if ($ returnValue === 1 && is_string ($ outputValue )) {
31- return new Response ($ outputValue , 200 );
32- } elseif (is_string ($ returnValue ) && is_string ($ outputValue ) && strlen ($ outputValue ) > 0 ) {
33- throw new \Exception ('In the PHP file the return value must be omitted if an output was made via echo: ' . $ path );
34+ $ title = html_extract_title ($ outputValue , basename ($ path ));
35+ return $ this ->html ('route-handler.html.twig ' , ['title ' => $ title , 'html ' => $ outputValue ]);
3436 } elseif (is_string ($ returnValue )) {
35- return new Response ($ returnValue , 200 );
37+ if (is_string ($ outputValue ) && strlen ($ outputValue ) > 0 ) {
38+ throw new \Exception ('In the PHP file the return value must be omitted if an output was made via echo: ' . $ path );
39+ }
40+ return new Response ($ returnValue , 200 );
3641 } elseif (is_array ($ returnValue )) {
3742 return $ this ->json ($ returnValue );
3843 } elseif ($ returnValue instanceof Response) {
@@ -56,6 +61,11 @@ public function json(array $context = []): Response
5661 ]);
5762 }
5863
64+ public function redirect (string $ url , int $ status = 302 ): Response
65+ {
66+ return new RedirectResponse ($ url , $ status );
67+ }
68+
5969 public function render (string $ template , array $ context = []): string
6070 {
6171 $ twig = $ this ->container ->get ('twig ' );
0 commit comments