Warning: Undefined variable $status in /www/wwwroot/akyelken.com/app/controller/base.php on line 267
Exception:Array
(
[message] => Vvveb\System\Core\FrontController::setStatus(): Argument #1 ($status) must be of type int, null given, called in /www/wwwroot/akyelken.com/app/controller/base.php on line 267
[file] => /www/wwwroot/akyelken.com/system/core/frontcontroller.php
[line_no] => 87
[line] => public static function setStatus(int $status): int { // <==
[lines] => Array
(
[0] => /**
[1] => * Sets the current HTTP status code.
[2] => *
[3] => * @param int $status
[4] => *
[5] => * @return int
[6] => */
[7] => public static function setStatus(int $status): int { // <==
[8] => return self::$status = $status;
[9] => }
[10] =>
[11] => /**
[12] => * Handles 404 (or other error) responses.
[13] => *
)
[trace] => #0 /www/wwwroot/akyelken.com/app/controller/base.php(267): Vvveb\System\Core\FrontController::setStatus()
#1 /www/wwwroot/akyelken.com/system/core/frontcontroller.php(220): Vvveb\Controller\Base->init()
#2 /www/wwwroot/akyelken.com/system/core/frontcontroller.php(329): Vvveb\System\Core\FrontController::call()
#3 /www/wwwroot/akyelken.com/system/core/frontcontroller.php(403): Vvveb\System\Core\FrontController::redirect()
#4 /www/wwwroot/akyelken.com/system/core/startup.php(525): Vvveb\System\Core\FrontController::dispatch()
#5 /www/wwwroot/akyelken.com/index.php(172): Vvveb\System\Core\start()
#6 /www/wwwroot/akyelken.com/public/index.php(28): include('...')
#7 {main}
[code] => Array
(
[0] =>
[2] => /**
[3] => * Vvveb
[4] => *
[5] => * Copyright (C) 2022 Ziadin Givan
[6] => *
[7] => * This program is free software: you can redistribute it and/or modify
[8] => * it under the terms of the GNU Affero General Public License as
[9] => * published by the Free Software Foundation, either version 3 of the
[10] => * License, or (at your option) any later version.
[11] => *
[12] => * This program is distributed in the hope that it will be useful,
[13] => * but WITHOUT ANY WARRANTY; without even the implied warranty of
[14] => * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
[15] => * GNU Affero General Public License for more details.
[16] => *
[17] => * You should have received a copy of the GNU Affero General Public License
[18] => * along with this program. If not, see .
[19] => *
[20] => */
[21] =>
[22] => namespace Vvveb\System\Core;
[23] =>
[24] => use function Vvveb\__;
[25] => use Vvveb\System\Event;
[26] => use Vvveb\System\PageCache;
[27] => use Vvveb\System\Routes;
[28] => use Vvveb\System\Session;
[29] =>
[30] => #[\AllowDynamicProperties]
[31] => class FrontController {
[32] => /**
[33] => * Current module (controller) name.
[34] => *
[35] => * @var string
[36] => */
[37] => private static string $moduleName = '';
[38] =>
[39] => /**
[40] => * Current action name.
[41] => *
[42] => * @var string
[43] => */
[44] => private static string $actionName = '';
[45] =>
[46] => /**
[47] => * Current HTTP status code.
[48] => *
[49] => * @var int
[50] => */
[51] => private static int $status = 200;
[52] =>
[53] => /**
[54] => * Returns current controller name.
[55] => *
[56] => * @return string
[57] => */
[58] => public static function getModuleName(): string {
[59] => return self::$moduleName;
[60] => }
[61] =>
[62] => /**
[63] => * Returns current action name.
[64] => *
[65] => * @return string
[66] => */
[67] => public static function getActionName(): string {
[68] => return self::$actionName;
[69] => }
[70] =>
[71] => /**
[72] => * Returns current HTTP status code.
[73] => *
[74] => * @return int
[75] => */
[76] => public static function getStatus(): int {
[77] => return self::$status;
[78] => }
[79] =>
[80] => /**
[81] => * Sets the current HTTP status code.
[82] => *
[83] => * @param int $status
[84] => *
[85] => * @return int
[86] => */
[87] => public static function setStatus(int $status): int { // <==
[88] => return self::$status = $status;
[89] => }
[90] =>
[91] => /**
[92] => * Handles 404 (or other error) responses.
[93] => *
[94] => * @param mixed $service
[95] => * @param string|array|false $message
[96] => * @param int $statusCode
[97] => *
[98] => * @return never
[99] => */
[100] => public static function notFound(mixed $service = false, string|array|false $message = false, int $statusCode = 404): never {
[101] => self::$status = $statusCode;
[102] =>
[103] => $file = DIR_APP . DS . 'controller' . DS . "error$statusCode.php";
[104] => if (file_exists($file)) {
[105] => $controller = 'Vvveb\Controller\Error' . $statusCode;
[106] => self::$moduleName = 'error' . $statusCode;
[107] => $view = View::getInstance();
[108] => if (is_array($message)) {
[109] => $view->set($message);
[110] => } else {
[111] => $view->message = $message;
[112] => }
[113] =>
[114] => $view->file = '';
[115] => $view->trace = [];
[116] =>
[117] => self::call($controller, 'index', $file);
[118] => PageCache::getInstance()->cleanUp();
[119] => exit();
[120] => } else {
[121] => PageCache::getInstance()->cleanUp();
[122] =>
[123] => http_response_code($statusCode);
[124] => exit("Http error $statusCode");
[125] => }
[126] => }
[127] =>
[128] => /**
[129] => * Extension point run after the response is sent.
[130] => *
[131] => * Currently a no-op: connection teardown is left to PHP shutdown.
[132] => *
[133] => * @return void
[134] => */
[135] => public static function closeConnections(): void {}
[136] =>
[137] => /**
[138] => * Inject dependencies into a controller instance.
[139] => *
[140] => * @param object &$controller
[141] => *
[142] => * @return void
[143] => */
[144] => public static function di(object &$controller): void {
[145] => $controller->request = Request::getInstance();
[146] => $controller->response = Response::getInstance();
[147] => $controller->view = View::getInstance();
[148] => $controller->session = Session::getInstance();
[149] => }
[150] =>
[151] => /**
[152] => * Builds the structured error payload used by dispatch failures.
[153] => *
[154] => * @param string $message Human readable error message.
[155] => * @param string|array $detail Extra context included with the error.
[156] => *
[157] => * @return array
[158] => */
[159] => private static function errorPayload(string $message, string|array $detail = []): array {
[160] => $payload = ['message' => $message];
[161] =>
[162] => if (is_array($detail)) {
[163] => $payload += $detail;
[164] => }
[165] =>
[166] => return $payload;
[167] => }
[168] =>
[169] => /**
[170] => * Route to the 404 handler for a missing controller method.
[171] => *
[172] => * @param string $controllerClass
[173] => * @param string $actionName
[174] => *
[175] => * @return never
[176] => */
[177] => private static function methodNotFound(string $controllerClass, string $actionName): never {
[178] => self::notFound(false, self :: errorPayload(
[179] => __('Method does not exist!'),
[180] => ['file' => "$controllerClass :: $actionName"]
[181] => ));
[182] => }
[183] =>
[184] => /**
[185] => * Initializes the controller class and calls the action.
[186] => *
[187] => * @param string $controllerClass
[188] => * @param string $actionName
[189] => * @param string|false $file
[190] => *
[191] => * @return mixed
[192] => */
[193] => public static function call(string $controllerClass, string $actionName, string|false $file = false): mixed {
[194] => if ((!@include_once (DIR_APP . DS . 'controller' . DS . 'base.php')) ||
[195] => (!file_exists($file) || !@include_once ($file))) {
[196] => return self::notFound(false, self :: errorPayload(__('Controller file not found!'), ['file' => $file]));
[197] => }
[198] =>
[199] => // We check if the controller's class really exists
[200] => $controller = false;
[201] =>
[202] => if (class_exists($controllerClass, false)) {// if the controller does not exist route to controller main
[203] => $controller = new $controllerClass();
[204] =>
[205] => if (!method_exists($controller, $actionName) ||
[206] => $actionName === 'init' || str_starts_with($actionName, '_')) {
[207] => return self::methodNotFound($controllerClass, $actionName);
[208] => }
[209] => } else {
[210] => return self::notFound(false, self :: errorPayload(
[211] => __('Controller does not exist!'),
[212] => $controllerClass
[213] => ));
[214] => }
[215] =>
[216] => self::di($controller);
[217] =>
[218] => if (method_exists($controller, 'init')) {
[219] => $return = $controller->init();
[220] =>
[221] => if ($return) {
[222] => $actionName = $return;
[223] =>
[224] => if (!method_exists($controller, $actionName) || str_starts_with($actionName, '_')) {
[225] => return self::methodNotFound($controllerClass, $actionName);
[226] => }
[227] => }
[228] => }
[229] =>
[230] => $response = Response::getInstance();
[231] => $template = str_replace('/', DS, strtolower(self::$moduleName));
[232] => $theme = $controller->view->getTheme();
[233] => $path = DIR_THEME . $theme . DS;
[234] => $isPlugin = str_starts_with($template, 'plugins/');
[235] =>
[236] => if ($actionName && $actionName !== 'index') {
[237] => if ($isPlugin) {
[238] => $t = str_replace('plugins' . DS, '', $template);
[239] => $p = str_contains($t, DS) ? strpos($t, DS) : false;
[240] => $pluginName = substr($t, 0, $p);
[241] => $nameSpace = substr($t, $p + 1);
[242] =>
[243] => $html = DIR_PLUGINS . $pluginName . DS . 'public' . DS . APP . DS . $nameSpace . DS . strtolower($actionName) . '.html';
[244] => } else {
[245] => $html = $path . $template . DS . strtolower($actionName) . '.html';
[246] => }
[247] =>
[248] => if (is_file($html)) {
[249] => $template .= DS . strtolower($actionName);
[250] => }
[251] => }
[252] =>
[253] => [$template, $controller, $actionName] = Event::trigger($controllerClass, $actionName . ':before', $template, $controller, $actionName);
[254] =>
[255] => $controller->view->template($template . '.html'); //default html that can be overwritten
[256] =>
[257] => $template = call_user_func([$controller, $actionName]);
[258] =>
[259] => [$template, $controller, $actionName] = Event::trigger(__CLASS__, __FUNCTION__, $template, $controller, $actionName);
[260] =>
[261] => $responseType = $response->getType();
[262] => $controller->view->setType($responseType);
[263] =>
[264] => if ($template === false) {
[265] => $controller->view->template(false);
[266] => } else {
[267] => if (is_array($template)) {
[268] => $response->output($template);
[269] => } else {
[270] => if ($template) {
[271] => $controller->view->template($template);
[272] => }
[273] => }
[274] => }
[275] =>
[276] => $response->setStatus(self::$status);
[277] => $return = $response->output();
[278] => self::closeConnections();
[279] =>
[280] => return $return;
[281] => }
[282] =>
[283] => /**
[284] => * Redirect or direct to a action or default controller action and parameters.
[285] => * It has the ability to http redirect to the specified action.
[286] => * Internally used to direct to action.
[287] => *
[288] => * @param string $moduleName
[289] => * @param string $actionName
[290] => *
[291] => * @return mixed
[292] => */
[293] => public static function redirect(string $moduleName, string $actionName = 'index'): mixed {
[294] => self::$moduleName = $moduleName;
[295] => self::$actionName = $actionName;
[296] =>
[297] => if (is_dir(DIR_APP . DS . 'controller' . DS . strtolower($moduleName))) {
[298] => self::$moduleName = $moduleName .= '/Index';
[299] => }
[300] =>
[301] => $dir = strtolower(str_replace('/', DS, $moduleName));
[302] =>
[303] => $className = \Vvveb\dashesToCamelCase(str_replace(['/', DS], '\\', $moduleName));
[304] => $controllerClass = 'Vvveb\Controller\\' . $className;
[305] =>
[306] => //change file paths for plugins
[307] => if (str_starts_with($moduleName, 'Plugins/')) {
[308] => $dir = str_replace('plugins' . DS, '', $dir);
[309] => $p = strpos($dir, DS);
[310] => $pluginName = $dir;
[311] => $nameSpace = 'index';
[312] =>
[313] => if ($p !== false) {
[314] => $pluginName = substr($dir, 0, $p);
[315] => $nameSpace = substr($dir, $p + 1);
[316] => }
[317] => //$className = str_replace('Plugins\\', '', $className);
[318] => $file = DIR_PLUGINS . $pluginName . DS . APP .
[319] => DS . 'controller' . DS . "$nameSpace.php";
[320] => $pluginName = \Vvveb\dashesToCamelCase($pluginName);
[321] => //insert Controller namespace
[322] => $className = str_replace('Plugins\\' . $pluginName, $pluginName . '\Controller', $className);
[323] => $controllerClass = 'Vvveb\Plugins\\' . $className;
[324] => } else {
[325] => $file = DIR_APP . 'controller' . DS . $dir . '.php';
[326] => }
[327] =>
[328] => return self::call($controllerClass, $actionName, $file);
[329] => }
[330] =>
[331] => /**
[332] => * Returns the current route from the query string.
[333] => *
[334] => * @return string
[335] => */
[336] => public static function getRoute(): string {
[337] => return $_GET['route'] ?? '';
[338] => }
[339] =>
[340] => /**
[341] => * Returns the current module from the query string.
[342] => *
[343] => * @return string
[344] => */
[345] => public static function getModule(): string {
[346] => return $_GET['module'] ?? '';
[347] => }
[348] =>
[349] => /**
[350] => * Dispatches the request to the appropriate controller/action.
[351] => *
[352] => * @param array &$site
[353] => *
[354] => * @return mixed
[355] => */
[356] => public static function dispatch(array &$site = []): mixed {
[357] => $module = $_GET['module'] ?? $_POST['module'] ?? null;
[358] => $action = $_GET['action'] ?? $_POST['action'] ?? null;
[359] => $_REQUEST = array_merge($_GET, $_REQUEST);
[360] =>
[361] => //remove GET parameters to allow correct matching,
[362] => $uri = SITE_URI;
[363] => $uri = preg_replace('/\?.*$/', '', $uri);
[364] =>
[365] => $route = false;
[366] => $routesSiteId = null;
[367] => if (isset($site['route'])) {
[368] => $routesSiteId = $site['site_id'];
[369] => }
[370] =>
[371] => if (!$module && (APP !== 'admin' && APP !== 'install' && (Routes::init(APP, $routesSiteId) && $route = Routes::match($uri)))) {
[372] => $_GET = array_merge($route, $_GET);
[373] => } else {
[374] => $module = $module ?? ((APP === 'install' || APP === 'admin') ? 'index' : false);
[375] =>
[376] => if (!$module && APP === 'app') {
[377] => return self::notFound(false, ['message' => 'No route!']);
[378] => }
[379] => }
[380] =>
[381] => if ($route) {
[382] => if (preg_match('@(^.+?)/(\w+$)@', $_GET['module'], $routeMatch)) {
[383] => $module = $routeMatch[1];
[384] => $action ??= $routeMatch[2];
[385] => } else {
[386] => $module = trim($_GET['module'], '/');
[387] => }
[388] => }
[389] =>
[390] => if (empty($action)) {
[391] => $action = 'index';
[392] => }
[393] => //santize inputs
[394] => if ((!preg_match('@^[a-zA-Z_/0-9\-]{4,70}$@', $module)) ||
[395] => (!preg_match('@^[a-zA-Z_/0-9\-]{3,70}$@', $action))) {
[396] => return self::notFound(false, ['message' => 'Invalid request!'], 500);
[397] => }
[398] =>
[399] => //capitalize each namespace segment of the module path (ex: content/posts -> Content/Posts)
[400] => $path = implode('/', array_map('ucfirst', explode('/', $module)));
[401] =>
[402] => return self::redirect($path, $action);
[403] => }
[404] => }
)
)