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

Laravel style Routing #4

Closed
pjebs opened this issue Dec 2, 2014 · 1 comment
Closed

Laravel style Routing #4

pjebs opened this issue Dec 2, 2014 · 1 comment

Comments

@pjebs
Copy link

pjebs commented Dec 2, 2014

Hi Daniel,

Can you give me a hint on what me be changed so that the route parameters are enclosed in {} and question marks can be used to make it optional just like in Laravel.

http://laravel.com/docs/4.2/routing#route-parameters

Route::get('user/{id}', function($id)
{
return 'User '.$id;
});

Optional Route Parameters

Route::get('user/{name?}', function($name = null)
{
return $name;
});

Optional Route Parameters With Defaults

Route::get('user/{name?}', function($name = 'John')
{
return $name;
});

Regular Expression Route Constraints

Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');

Route::get('user/{id}', function($id)
{
//
})
->where('id', '[0-9]+');

@dimfeld
Copy link
Owner

dimfeld commented Dec 2, 2014

Line 97 of tree.go is probably a good place to start for changing the :token style to {token}. You can look for a { character instead and then filter out the ? and/or } at the end of the token too.

Adding the optional capability is a little more complex. The current system just returns the final node from addPath, and then the Handle function adds the handler to the returned node. The simplest possibility would be to change addPath to take the verb, handler function, and addSlash as arguments, and update the nodes appropriately as they go. Then when you see a ? in the next token, you can just call n.setHandler(verb, handler) to set the handler on the current node in addition to setting it on the child node.

I wasn't sure from the questions If you want regexes or not, since the examples you gave use them but you didn't mention them otherwise. The data structure used by httptreemux doesn't lend itself well to regexes, although you could probably have a special node type for it. But there are some frameworks that already support regexes too which would be a better choice.

@dimfeld dimfeld closed this as completed Dec 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants