Skip to content

Commit

Permalink
Fixed method checking in routes
Browse files Browse the repository at this point in the history
  • Loading branch information
eugabrielsilva committed Sep 27, 2024
1 parent 54fa3b4 commit fcf1513
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/CLI/Firefly.php
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,8 @@ private static function __help()
self::print(' <color="yellow">init</color> | Initializes the project');
self::print(' <color="yellow">shine</color> <color="blue">--host --port</color> | Starts the local development server');
self::print(' <color="yellow">sandbox</color> | Starts the REPL interactive mode');
self::print(' <color="yellow">down</color> | Puts the application under maintenance mode.');
self::print(' <color="yellow">up</color> | Removes the application from maintenance mode.');
self::print(' <color="yellow">down</color> | Puts the application under maintenance mode');
self::print(' <color="yellow">up</color> | Removes the application from maintenance mode');
self::print(' <color="yellow">routes</color> | Prints a list of all application routes');
self::print(' <color="yellow">clear-cache</color> | Clears the application cache folder');
self::print(' <color="yellow">clear-session</color> | Clears the application session folder');
Expand Down
39 changes: 9 additions & 30 deletions src/Http/Rails.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ public static function addProtectedRoute(string $route, $middleware = 'Glowie\Mi
/**
* Setup an anonymous (controller-independent) protected route for the application.
* @param string $route The route URI to setup.
* @param Closure $callback A closure to run anonymously. A generic controller instance will be injected as a param of this function.
* @param string|array $middleware (Optional) The namespaced middleware name that this route will use to protect itself.\
* You can use `MiddlewareName::class` to get this property correctly. You can also use an array of multiple middlewares.
* @param Closure $callback A closure to run anonymously. A generic controller instance will be injected as a param of this function.
* @param string|array $methods (Optional) HTTP methods that this route accepts. Can be a single method or an array of methods.\
* Leave empty for all.
* @param string $name (Optional) Route name.
*/
public static function addProtectedAnonymous(string $route, $middleware = 'Glowie\Middlewares\Authenticate', Closure $callback, $methods = [], string $name = '')
public static function addProtectedAnonymous(string $route, Closure $callback, $middleware = 'Glowie\Middlewares\Authenticate', $methods = [], string $name = '')
{
if (Util::isEmpty($name)) $name = Util::slug($route, '-', true);
if (Util::isEmpty($middleware)) throw new RoutingException('Middleware cannot be empty for route "' . $name . '"');
Expand Down Expand Up @@ -304,7 +304,7 @@ public static function domain(string $domain, Closure $callback)

/**
* Sets a middleware to all application routes.
* @param string|array $middleware (Optional) The namespaced middleware name that all routes will use to protect themself.\
* @param string|array $middleware The namespaced middleware name that all routes will use to protect themself.\
* You can use `MiddlewareName::class` to get this property correctly. You can also use an array of multiple middlewares.
*/
public static function setGlobalMiddleware($middleware)
Expand Down Expand Up @@ -445,12 +445,6 @@ public static function init()
self::$currentParams = $config['params']->toArray();
self::$currentRoute = $config['name'];

// Checks if there is a request method configuration
if (!empty($config['methods'])) {
$config['methods'] = array_map('strtoupper', $config['methods']);
if (!in_array(self::$request->getMethod(), $config['methods'])) return self::callMethodNotAllowed();
}

// Checks if there is a redirect configuration
if (!empty($config['redirect'])) return self::$response->redirect($config['redirect'], $config['code']);

Expand Down Expand Up @@ -561,6 +555,12 @@ public static function matchRoute(string $uri)
// Check route domain
if (!is_null($item['domain']) && self::$request->getHostname() !== $item['domain']) continue;

// Checks if there is a request method configuration
if (!empty($item['methods'])) {
$item['methods'] = array_map('strtoupper', $item['methods']);
if (!in_array(self::$request->getMethod(), $item['methods'])) continue;
}

// Parse route parameters
$result = [];
if (!empty($params)) {
Expand Down Expand Up @@ -623,27 +623,6 @@ private static function callForbidden()
self::$controller->forbidden();
}

/**
* Calls `methodNotAllowed()` action in Error controller.
*/
private static function callMethodNotAllowed()
{
// Check if method is implemented
self::$response->setStatusCode(Response::HTTP_METHOD_NOT_ALLOWED);
$controller = 'Glowie\Controllers\Error';
if (!class_exists($controller) || !is_callable([$controller, 'methodNotAllowed'])) {
return self::loadDefaultErrorView([
'title' => 'Method Not Allowed',
'text' => '405 | Method Not Allowed'
]);
}

// Create Error controller and dispatch method
self::$controller = new $controller;
if (is_callable([self::$controller, 'init'])) self::$controller->init();
self::$controller->methodNotAllowed();
}

/**
* Calls `serviceUnavailable()` action in Error controller.
*/
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.6.2
1.7.0

0 comments on commit fcf1513

Please sign in to comment.