Skip to content

Commit

Permalink
Merge pull request #11 from Mohammadreza-73/1.x
Browse files Browse the repository at this point in the history
[1.x] Add middleware feature (#8)
  • Loading branch information
Mohammadreza-73 authored Apr 20, 2023
2 parents 9f33fb4 + 24ffc77 commit 0175031
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
14 changes: 14 additions & 0 deletions app/Core/Http/Middleware/Authenticated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Core\Http\Middleware;

class Authenticated
{
public function handle()
{
if (! $_SESSION['user'] ?? false) {
header('location: /');
exit();
}
}
}
1 change: 1 addition & 0 deletions app/Core/Http/Middleware/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Middleware
{
public const MAP = [
'guest' => Guest::class,
'auth' => Authenticated::class,
];

public static function resolve(string $key)
Expand Down
8 changes: 5 additions & 3 deletions app/Core/Http/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Route
* @param string $method
* @param string $uri
* @param Closure|string|array $controller
* @return void
* @return static
*/
public static function add(string $method, string $uri, $controller)
{
Expand All @@ -23,7 +23,7 @@ public static function add(string $method, string $uri, $controller)
'middleware' => null,
];

return new static;
return new static();
}

public static function get(string $uri, $controller)
Expand Down Expand Up @@ -58,6 +58,8 @@ public static function all()

public function middleware(string $key)
{

self::$routes[array_key_last(self::$routes)]['middleware'] = $key;

return new static;
}
}
5 changes: 4 additions & 1 deletion app/Core/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Core\Http;

use Closure;
use App\Core\Http\Middleware\Middleware;
use App\Core\Exceptions\ClassNotFoundException;
use App\Core\Exceptions\MethodNotFoundException;

Expand Down Expand Up @@ -30,7 +31,9 @@ private function findRoute(Request $request)
{
foreach ($this->routes as $route) {
if ($request->method === $route['method'] && $this->isUriMatched($route)) {
return $route;
Middleware::resolve($route['middleware']);

return $route;
}
}

Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public function index()
{
return view('home');
}

public function dashboard()
{
echo 'admin dashboard';
}
}
1 change: 1 addition & 0 deletions routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
use App\Core\Http\Route;

Route::get('/', ['HomeController', 'index'])->middleware('guest');
Route::get('/admin', ['HomeController', 'dashboard'])->middleware('auth');

0 comments on commit 0175031

Please sign in to comment.