Fixed Routes

main
Robert 7 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) == '__') {
$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") &&

@ -19,10 +19,12 @@ use \CodeHydrater\http\response as Response;
*
* @author Robert Strutts <Bob_586@Yahoo.com>
*/
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,
@ -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() {
$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');

@ -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 {

@ -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;
}
);

@ -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];
}
}
}

Loading…
Cancel
Save