Fixed Routes

main
Robert 8 hours ago
parent dbfa82ef81
commit d4c5a7220c
  1. 5
      src/classes/app.php
  2. 23
      src/classes/base_controller.php
  3. 1
      src/classes/html_document.php
  4. 13
      src/classes/http/response.php
  5. 10
      src/classes/http/route_service_provider.php
  6. 6
      src/classes/router.php

@ -135,6 +135,9 @@ class app {
if (substr($method, 0, 2) == '__') { if (substr($method, 0, 2) == '__') {
$method = ""; // Stop any magical methods being called $method = ""; // Stop any magical methods being called
} }
if ($method == "init") {
$method = ""; // Stop init methods from being called
}
if ($is_controller === true) { if ($is_controller === true) {
$this->file = $file; $this->file = $file;
@ -175,7 +178,7 @@ class app {
$call_class = "\\Project\\" . $test . 'controllers\\' . $class; $call_class = "\\Project\\" . $test . 'controllers\\' . $class;
$controller = new $call_class($this->request, $this->response); $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 ?? []; $controller_middleware = $controller::$middleware ?? [];
if ($method === "error" && str_contains($class, "app") && if ($method === "error" && str_contains($class, "app") &&

@ -19,10 +19,12 @@ use \CodeHydrater\http\response as Response;
* *
* @author Robert Strutts <Bob_586@Yahoo.com> * @author Robert Strutts <Bob_586@Yahoo.com>
*/ */
abstract class base_controller { class base_controller {
public static $params; // To keep Routes working...
protected $view; protected $view;
protected $html; protected $html;
protected string $footer = "";
protected string $authors = "";
public function __construct( public function __construct(
protected Request $request, protected Request $request,
@ -37,4 +39,21 @@ abstract class base_controller {
} }
} }
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);
}
} }

@ -34,6 +34,7 @@ class html_document {
public function __construct() { public function __construct() {
$this->title = bootstrap\configure::get('html', 'title') ?? ''; $this->title = bootstrap\configure::get('html', 'title') ?? '';
$this->author = bootstrap\configure::get('html', 'author') ?? ''; $this->author = bootstrap\configure::get('html', 'author') ?? '';
$this->footer = bootstrap\configure::get('html', 'footer') ?? '';
$this->keywords = bootstrap\configure::get('html', 'keywords') ?? ''; $this->keywords = bootstrap\configure::get('html', 'keywords') ?? '';
$this->description = bootstrap\configure::get('html', 'description') ?? ''; $this->description = bootstrap\configure::get('html', 'description') ?? '';
$this->robots = bootstrap\configure::get('html', 'robots'); $this->robots = bootstrap\configure::get('html', 'robots');

@ -21,7 +21,7 @@ class response
protected array $headers = [] protected array $headers = []
) { } ) { }
public function send(): void { public function send(bool $backtrace = false): void {
http_response_code($this->status_code); http_response_code($this->status_code);
foreach ($this->headers as $name => $value) { foreach ($this->headers as $name => $value) {
@ -29,7 +29,16 @@ class response
} }
if ($this->status_code > 499) { 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) { } elseif ($this->json) {
echo $this->json; echo $this->json;
} else { } else {

@ -44,7 +44,15 @@ class route_service_provider extends ServiceProvider {
}; };
}, },
function ($request, $response) use ($handler) { 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; return $response;
} }
); );

@ -46,7 +46,7 @@ class router
* *
* @param $name * @param $name
*/ */
public function name($name) public static function name($name)
{ {
$route = self::$routes[self::$last]; $route = self::$routes[self::$last];
unset(self::$routes[self::$last]); unset(self::$routes[self::$last]);
@ -439,7 +439,7 @@ class router
if (!empty($parentControllers)) { if (!empty($parentControllers)) {
end($parentControllers); end($parentControllers);
$parentController = $parentControllers[key($parentControllers)]; $parentController = $parentControllers[key($parentControllers)];
$parentController = new $parentController; $parentController = new $parentController($my_request, $my_response);
// Add properties to parent class // Add properties to parent class
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
@ -450,7 +450,7 @@ class router
//Call method //Call method
if (method_exists($controller, $method)) { if (method_exists($controller, $method)) {
$returned = call_user_func_array([$controller, $method], $params); $returned = call_user_func_array([$controller, $method], $params);
return ["found"=> true, "returned"=> $returned, "middleware"=>$controller_middlewares]; return ["found"=> true, "returned"=> $returned, "middleware"=>$controller_middleware];
} }
} }
} }

Loading…
Cancel
Save