Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable HTTP Verb in Routing #2712

Merged
merged 2 commits into from
Nov 11, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion system/core/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,46 @@ protected function _parse_routes()
// Turn the segment array into a URI string
$uri = implode('/', $this->uri->segments);

// Get HTTP verb
$http_verb = strtolower($_SERVER['REQUEST_METHOD']);

// Is there a literal match? If so we're done
if (isset($this->routes[$uri]) && is_string($this->routes[$uri]))
if (isset($this->routes[$uri][$http_verb]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should first check if $this->routes[$uri] exists and then have the other conditions depending on it, i.e.:

if (isset($this->routes[$uri]))
{
    if (is_string($this->routes[$uri]))
    {
        // regular route code
    }
    elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
    {
        // verb route code
    }
    ...
}

{
return $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
}
else if (isset($this->routes[$uri]['(:any)']))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if -> elseif

That's described in our styleguide, please take the time to read it.

{
return $this->_set_request(explode('/', $this->routes[$uri]['(:any)']));
}
// Fallback to default routing
else if (isset($this->routes[$uri]) && is_string($this->routes[$uri]))
{
return $this->_set_request(explode('/', $this->routes[$uri]));
}

// Loop through the route array looking for wildcards
foreach ($this->routes as $key => $val)
{
// Check if HTTP Verb is exist
if (is_array($val))
{
// HTTP verb included in routes
if (isset($val[$http_verb]))
{
$val = $val[$http_verb];
}
else if (isset($val['(:any)']))
{
$val = $val['(:any)'];
}
else
{
// HTTP Verb not found
continue;
}
}

// Convert wildcards to RegEx
$key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);

Expand Down