diff --git a/src/classes/app.php b/src/classes/app.php index b607bf8..511b918 100644 --- a/src/classes/app.php +++ b/src/classes/app.php @@ -135,6 +135,9 @@ class app { if (substr($method, 0, 2) == '__') { $method = ""; // Stop any magical methods being called } + if ($method == "init") { + $method = ""; // Stop init methods from being called + } if ($is_controller === true) { $this->file = $file; @@ -175,7 +178,7 @@ class app { $call_class = "\\Project\\" . $test . 'controllers\\' . $class; $controller = new $call_class($this->request, $this->response); - // Collect controller-level middleware + // Collect controller-level middleware Directly from the controller file, IE: public static array $middleware = [ \Project\classes\auth_middleware::class ]; $controller_middleware = $controller::$middleware ?? []; if ($method === "error" && str_contains($class, "app") && diff --git a/src/classes/base_controller.php b/src/classes/base_controller.php index c6d2233..b7e23e1 100644 --- a/src/classes/base_controller.php +++ b/src/classes/base_controller.php @@ -19,11 +19,13 @@ use \CodeHydrater\http\response as Response; * * @author Robert Strutts */ -abstract class base_controller { - + class base_controller { + public static $params; // To keep Routes working... protected $view; protected $html; - + protected string $footer = ""; + protected string $authors = ""; + public function __construct( protected Request $request, protected Response $response @@ -36,5 +38,22 @@ abstract class base_controller { $this->init(); } } + + public function set_footer_and_authors() { + if (defined('\Project\MyPage::MyFooter')) { + $this->footer = \Project\MyPage::MyFooter; + } else { + $this->footer = ""; + } + + if (defined('\Project\MyPage::MyAuthors')) { + $this->authors = \Project\MyPage::MyAuthors; + } else { + $this->authors = ""; + } + + $this->html->set_footer($this->footer); + $this->html->set_author($this->authors); + } } diff --git a/src/classes/html_document.php b/src/classes/html_document.php index bd6920e..a0ff79b 100644 --- a/src/classes/html_document.php +++ b/src/classes/html_document.php @@ -34,6 +34,7 @@ class html_document { public function __construct() { $this->title = bootstrap\configure::get('html', 'title') ?? ''; $this->author = bootstrap\configure::get('html', 'author') ?? ''; + $this->footer = bootstrap\configure::get('html', 'footer') ?? ''; $this->keywords = bootstrap\configure::get('html', 'keywords') ?? ''; $this->description = bootstrap\configure::get('html', 'description') ?? ''; $this->robots = bootstrap\configure::get('html', 'robots'); diff --git a/src/classes/http/response.php b/src/classes/http/response.php index d3dd1a0..fbde1ba 100644 --- a/src/classes/http/response.php +++ b/src/classes/http/response.php @@ -21,7 +21,7 @@ class response protected array $headers = [] ) { } - public function send(): void { + public function send(bool $backtrace = false): void { http_response_code($this->status_code); foreach ($this->headers as $name => $value) { @@ -29,7 +29,16 @@ class response } if ($this->status_code > 499) { - throw new \Exception($this->content); + if (is_string($this->content) && ! empty($this->content)) { + if ($backtrace) { + dd(debug_backtrace()); + } else { + throw new \Exception($this->content); + } + } else { + throw new \Exception("Status Code #" . $this->status_code); + } + } elseif ($this->json) { echo $this->json; } else { diff --git a/src/classes/http/route_service_provider.php b/src/classes/http/route_service_provider.php index 3ff7c9b..037c5b1 100644 --- a/src/classes/http/route_service_provider.php +++ b/src/classes/http/route_service_provider.php @@ -44,7 +44,15 @@ class route_service_provider extends ServiceProvider { }; }, function ($request, $response) use ($handler) { - $response->set_content($handler->get_content()); + try { + $data = $handler->get_content(); + } catch (\Throwable $e) { + throw new \Exception("No Response from [Controller]?"); + } + if (! empty($data)) { + $response->set_content($data); + } + return $response; } ); diff --git a/src/classes/router.php b/src/classes/router.php index 90df4bc..50a3da5 100644 --- a/src/classes/router.php +++ b/src/classes/router.php @@ -46,7 +46,7 @@ class router * * @param $name */ - public function name($name) + public static function name($name) { $route = self::$routes[self::$last]; unset(self::$routes[self::$last]); @@ -439,7 +439,7 @@ class router if (!empty($parentControllers)) { end($parentControllers); $parentController = $parentControllers[key($parentControllers)]; - $parentController = new $parentController; + $parentController = new $parentController($my_request, $my_response); // Add properties to parent class foreach ($params as $key => $value) { @@ -450,7 +450,7 @@ class router //Call method if (method_exists($controller, $method)) { $returned = call_user_func_array([$controller, $method], $params); - return ["found"=> true, "returned"=> $returned, "middleware"=>$controller_middlewares]; + return ["found"=> true, "returned"=> $returned, "middleware"=>$controller_middleware]; } } }