Example #1
0
 public function run()
 {
     try {
         if (isset($_SERVER['REQUEST_METHOD'])) {
             $request_method = $_SERVER['REQUEST_METHOD'];
             $request_method = Request::isAjax() ? 'AJAX' : $request_method;
             $inputFlag = Request::hasParam('_token');
             // check the request method if PUT, DELETE or POST
             if ($request_method == 'POST') {
                 if (isset($_POST['_method'])) {
                     $request_method = $_POST['_method'];
                 }
             }
             // check if the request method not supported
             if (!in_array($request_method, ['POST', 'GET', 'PUT', 'AJAX', 'DELETE'])) {
                 throw new BadRequestException('Unauthorized: Access is denied, REQUEST_METHOD not found');
             }
             $res = null;
             // if any routes are set with the request method
             if (isset($this->routes[$request_method])) {
                 foreach ($this->routes[$request_method] as $route) {
                     // find the route that matches the requested url
                     if ($route->equals($this->url)) {
                         // if the token field is set check the token
                         if ($route->token) {
                             $tokenFlag = Token::match(Request::getParam('_token'));
                             if (!$inputFlag || $inputFlag && !$tokenFlag) {
                                 throw new TokenMissMatchException('Unauthorized: Access is denied, Token Miss Match!');
                                 die('Token missmatch!');
                             }
                         }
                         // executes the requested route
                         $res = $route->exec();
                         if (is_string($res)) {
                             echo $res;
                         } else {
                             if (!is_null($res)) {
                                 dd($res);
                             }
                         }
                         return;
                     }
                 }
             }
             Response::error(404);
         } else {
             throw new BadRequestException('Unauthorized: Access is denied, REQUEST_METHOD not found');
         }
     } catch (Exception $exc) {
         die($exc->getMessage() . ' please go <a href="' . Request::getPrevUrl() . '">back.</a>');
     }
 }