diff --git a/Herbert/Framework/Middleware.php b/Herbert/Framework/Middleware.php new file mode 100644 index 0000000..3baa0b0 --- /dev/null +++ b/Herbert/Framework/Middleware.php @@ -0,0 +1,62 @@ +request = $http; + return $this->handleRequest(); + } + + /** + * To Handling the Request with Specific Middlewares. + */ + protected function handleRequest() + { + $request = $this->request; + if ($this->hasMiddleware()) { + $middleware = $this->request->middleware; + + if (class_exists($middleware)) { + $request = new $middleware; + $request = $request->handle($this->request); + } + } + return $request; + } + + + /** + * To Check Middleware Existence. + * + * @return bool + */ + public function hasMiddleware() + { + return (isset($this->request->middleware) && !empty($this->request->middleware)); + } +} diff --git a/Herbert/Framework/Route.php b/Herbert/Framework/Route.php index 457fd73..5b530b9 100644 --- a/Herbert/Framework/Route.php +++ b/Herbert/Framework/Route.php @@ -33,6 +33,11 @@ class Route { * @var string */ protected $uses; + + /** + * @var string + */ + protected $middleware; /** * @param \Herbert\Framework\Application $app @@ -46,6 +51,7 @@ public function __construct(Application $app, $data, $parameters = []) $this->uri = $data['uri']; $this->name = array_get($data, 'as', $this->uri); $this->uses = $data['uses']; + $this->middleware = $data['middleware']; } /** diff --git a/Herbert/Framework/Router.php b/Herbert/Framework/Router.php index 5b41a07..beff62b 100644 --- a/Herbert/Framework/Router.php +++ b/Herbert/Framework/Router.php @@ -292,7 +292,11 @@ protected function buildRoute($data, $params) */ protected function processRequest(Route $route) { + // Middleware Implementations + $route = (new Middleware())->init($route); + $this->processResponse($route->handle()); + } /**