Skip to content

Commit

Permalink
[ENH] refactoring routes path based on new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Sep 22, 2024
1 parent 2ed0ea9 commit 16b122b
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Wepesi\Core\Http\Redirect;
use Wepesi\Core\Session;

class indexController extends Controller
class exampleController extends Controller
{
public function __construct()
{
Expand Down
4 changes: 1 addition & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -31,6 +31,4 @@

$app = new Application($ROOT_DIR, $configuration);

require_once $app::getRootDir() . '/router/route.php';

$app->run();
6 changes: 0 additions & 6 deletions router/api.php

This file was deleted.

10 changes: 10 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

global $app;

use Wepesi\Core\Http\Response;

$router = $app->router();
$router->get('/', function () {
Response::send('Welcome to Wepesi API');
});
17 changes: 13 additions & 4 deletions router/route.php → routes/web.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/
global $app;

use Wepesi\Controller\indexController;
use Wepesi\Controller\exampleController;
use Wepesi\Core\Http\Response;
use Wepesi\Core\Views\View;
use Wepesi\Middleware\Validation\exampleValidation;

$router = $app->router();

// setup get started pages index
$router->get('/', function () {
(new View)->display('/home');
Expand All @@ -13,9 +19,12 @@
$router->get('/helloworld', function () {
(new View)->renderHTML('<h1>Hello World!</h1>');
});
$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';
$router->set404(function(){
Response::send('route not defined', 404);
});
68 changes: 64 additions & 4 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 16b122b

Please sign in to comment.