diff --git a/controller/indexController.php b/controller/exampleController.php similarity index 92% rename from controller/indexController.php rename to controller/exampleController.php index 3e4566c..6dceb06 100644 --- a/controller/indexController.php +++ b/controller/exampleController.php @@ -16,7 +16,7 @@ use Wepesi\Core\Http\Redirect; use Wepesi\Core\Session; -class indexController extends Controller +class exampleController extends Controller { public function __construct() { diff --git a/index.php b/index.php index 350e249..7413128 100644 --- a/index.php +++ b/index.php @@ -17,7 +17,7 @@ (new DotEnv($ROOT_DIR . '/.env'))->load(); /** - * Generate and index file for redirection (protection) while APP_DEV in production + * Generate and index a file for redirection (protection) while APP_DEV in production */ if (getenv('APP_ENV') === 'prod') { autoIndexFolder(['assets']); @@ -31,6 +31,4 @@ $app = new Application($ROOT_DIR, $configuration); -require_once $app::getRootDir() . '/router/route.php'; - $app->run(); diff --git a/router/api.php b/router/api.php deleted file mode 100644 index 9be1e80..0000000 --- a/router/api.php +++ /dev/null @@ -1,6 +0,0 @@ -api('/', function () use ($router) { - $router->get('/home', function () { - \Wepesi\Core\Http\Response::send(['message' => 'Welcom to api routing']); - }); -}); diff --git a/routes/api.php b/routes/api.php new file mode 100644 index 0000000..1406985 --- /dev/null +++ b/routes/api.php @@ -0,0 +1,10 @@ +router(); +$router->get('/', function () { + Response::send('Welcome to Wepesi API'); +}); \ No newline at end of file diff --git a/router/route.php b/routes/web.php similarity index 52% rename from router/route.php rename to routes/web.php index fc00b35..6197dc0 100644 --- a/router/route.php +++ b/routes/web.php @@ -1,10 +1,16 @@ router(); + // setup get started pages index $router->get('/', function () { (new View)->display('/home'); @@ -13,9 +19,12 @@ $router->get('/helloworld', function () { (new View)->renderHTML('

Hello World!

'); }); -$router->get('/home', [\Wepesi\Controller\indexController::class,'home']); + +$router->get('/home', [exampleController::class,'home']); // -$router->post('/changelang', [indexController::class, 'changeLang']) +$router->post('/changelang', [exampleController::class, 'changeLang']) ->middleware([exampleValidation::class, 'changeLang']); -include \Wepesi\Core\Application::getRootDir() . './router/api.php'; \ No newline at end of file +$router->set404(function(){ + Response::send('route not defined', 404); +}); diff --git a/src/Core/Application.php b/src/Core/Application.php index 41b2bb7..4dedffd 100644 --- a/src/Core/Application.php +++ b/src/Core/Application.php @@ -120,11 +120,11 @@ public static function setViewFolder(string $folder_name) } /** - * @return string|null + * @return string */ - public static function getLayout(): ?string + public static function getLayout(): string { - return strlen(trim(self::$layout )) > 0 ? self::$layout : null; + return trim(self::$layout ); } /** @@ -153,9 +153,69 @@ public function router(): Router /** * @return void + * @throws \Exception + */ + protected function routeProvider(): void + { + $base_route_path = self::getRootDir() . '/routes'; + $api_route_path = $base_route_path . '/api.php'; + if (file_exists($api_route_path)) { + $this->router->group([ + 'pattern' => '/api' + ], function (Router $router) { + if (isset($_SERVER['HTTP_ORIGIN'])) { + // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one + // you want to allow, and if so: + header('Access-Control-Allow-Origin: *'); + header('Access-Control-Allow-Credentials: true'); + header('Access-Control-Max-Age: 86400'); // cache for 1 day + } + header('Access-Control-Allow-Methods: GET, POST,PUT, PATCH, HEAD, OPTIONS'); + // Access-Control headers are received during OPTIONS requests + if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { + // may also be using PUT, PATCH, HEAD etc. + if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) + header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); + + exit(0); + } + $router->group([], $this->registerRoute('/api.php')); + }); + } + $web_route_path = $base_route_path . '/web.php'; + if (file_exists($web_route_path)) { + $this->router->group([], $this->registerRoute('/web.php')); + } + if (!file_exists($web_route_path) && !file_exists($api_route_path)) { + throw new \Exception('No Route file not found.'); + } + } + + /** + * route path + * @param string $path + * @return string + */ + public function registerRoute(string $path): string + { + return $this->basePath('/routes' . '/' . trim($path,'/')); + } + /** + * @param string $path + * @return string + */ + public function basePath(string $path): string + { + return self::$root_dir . '/' . trim($path,'/'); + } + + /** + * @return void + * @throws \Exception */ - public function run() + public function run(): void { + $this->routeProvider(); $this->router->run(); } } \ No newline at end of file